I try to write a function, that returns a promise version for some API (AgileCRMManager). The design of the api works pretty similar to request.
But i have some Problems with the handover of the Function. The Function have no access to the prototype of its own. I got following log output:
[Function: getContactByEmail]
[Function: getContactByEmail]
TypeError: this.getOptions is not a function
at getContactByEmail (/Users/Tilman/Documents/Programme/NodeJS/async_test/node_modules/agile_crm/agilecrm.js:116:24)
at /Users/Tilman/Documents/Programme/NodeJS/async_test/routes/portal.js:30:5
at restPromise (/Users/Tilman/Documents/Programme/NodeJS/async_test/routes/portal.js:29:10)
at Object.<anonymous> (/Users/Tilman/Documents/Programme/NodeJS/async_test/routes/portal.js:22:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
This is the part with getOptions from agile_crm:
ContactAPI.prototype.getOptions = function getOptions() {
this._options = {
host: this.domain,
headers: {
'Authorization': 'Basic ' + new Buffer(this.email + ':' + this.key).toString('base64'),
'Accept': 'application/json'
}
};
return this._options;
};
This is my Code (if i change restFunction with a.contactAPI.getContactByEmail, it works. But i want to have it for more functions):
var AgileCRMManager = require("agile_crm")
var a = new AgileCRMManager("user-domain",
"api-key",
"user-mail")
restPromise('ds@umzuege-selisch.de',a.contactAPI.getContactByEmail)
.then(console.log)
.catch(console.error)
function restPromise(data, restFunction) {
console.log(restFunction); // => [Function: getContactByEmail]
console.log(a.contactAPI.getContactByEmail); // => [Function: getContactByEmail]
return new Promise(function(fulfill, reject){
//a.contactAPI.getContactByEmail(
restFunction(
data,
function(data){
fulfill(data)
},
function(error){
reject(new Error(error))
}
)
})
}
Any Idea how i can hand Over the function and api will still work?