I am trying to connect to Recurly API in backend service file, then return as a new promise to backend controller. For some reason my code does't work.
I keep receiving this error message:
0|account- | TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined
This is my code, the API configuration and other setup works fine. Keep in mind this is part of the project code.
Service.js:
const Recurly = require('recurly-js');
function TeamService({}) {
this.updateBillingInfo = (team_id, body) => {
const recurly = new Recurly(
{
API_KEY: config.recurly.API_KEY,
SUBDOMIAN: config.recurly.SUBDOMIAN,
ENVIRONMENT: config.recurly.ENVIRONMENT,
DEBUG: config.recurly.DEBUG,
API_VERSION: config.recurly.API_VERSION
}
);
return new Promise((resolve, reject) => {
let res;
let err;
recurly.billingInfo.update(team_id, { token_id: body.id }, function (errResponse, response) {
if (errResponse) {
err = errResponse.data.error;
}
if(response){
res = response;
}
});
resolve(res);
reject(err);
});
};
}
}
Controller.js:
function Controller({ teamService, httpStatus, }) {
this.updateBillingInfo = (req, res) => {
const {
team_id,
body
} = req;
teamService.updateBillingInfo(team_id, body).then(function(result){
console.log(result);
return httpStatus.twoHundredSuccess(res, {result});
}).catch(function(err){
console.log(err);
httpStatus.fiveHundredError(res, err);
});
}
}
I expect that the function in service works get information from API. return as a new promise to controller file. But it is not working in service.js. I am not very good with promise. Please let me know which part I did wrong.