0

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.

Tian Qin
  • 153
  • 1
  • 3
  • 15

1 Answers1

1

Your resolve and reject should be inside the callback of async function:

recurly.billingInfo.update(team_id, { token_id: body.id }, function(
    errResponse,
    response
  ) {
    if (errResponse) {
      reject(errResponse.data.error);
    }else{
      resolve(response);
    }
  });
ambianBeing
  • 3,449
  • 2
  • 14
  • 25
  • It must be an `if`/`else`. With two `if`s, you could run into a situation where neither `resolve` nor `reject` is called. – Bergi Aug 02 '19 at 21:32
  • @Bergi Ideally yes there should be an `if/else` inside `error-callback`, thats a general practice & should be that way. But to think of it in this particular case I don't think there will ever be situation where none of the if passes. Isn't it? – ambianBeing Aug 02 '19 at 21:44
  • Well we both can't think of a situation within callback conventions where it would happen, but still why make the code error-prone when even in the expected case an `if`/`else` would be much simpler and work the same? – Bergi Aug 02 '19 at 21:47
  • 1
    Yeah, I'll edit the answer and thank you for pointig it out. Its better that way. – ambianBeing Aug 02 '19 at 21:49