-2

I am trying to build a backend script to call recurly api to load data and store it. I declare the function and assigned to getAccountInfo, after that I tried to call the function, but console says getAccountInfo is not a funtion.. And also console.log(accountInfo) display undefined. But I can watch it inside the function declaration.

The api parameter required the callback function, that is why it looks like this.

var accountInfo = {};


let getAccountInfo = recurly.accounts.list(function (errResponse, response) {
    if (errResponse) {
        reject(errResponse);
    }
    if (response) {
        accountInfo = response.data.accounts.account;
        resolve(response);      
    }
}, );

getAccountInfo();
console.log(accountInfo);

I expect that I can run the function and get the accountInfo. I am new to javascript and Node js, is that any concept that I misunderstood? Thank you very much for the help.

Tian Qin
  • 153
  • 1
  • 3
  • 15

2 Answers2

0

getAccountInfo is whatever recurly.accounts.list() returns, which isn't a function (since it's an asynchronous function, it might not return anything, or might return a Promise). You need to define a function yourself:

function getAccountInfo() {
  recurly.accounts.list(function(errResponse, response) {
    if (errResponse) {
      reject(errResponse);
    }
    if (response) {
      accountInfo = response.data.accounts.account;
      resolve(response);
    }
  });
}

Note that your console.log(accountInfo) won't print the result, since the function is asynchronous. See How do I return the response from an asynchronous call? for proper ways to structure the code so you can use the result.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I declare the function and assigned to getAccountInfo

No, that's not what you did. You called the function recurly.accounts.list() and set getAccountInfo to the value it returned. Since it's an asynchronous function, it probably returned almost immediately and there's a good chance it didn't return anything and getAccountInfo is set to undefined.

Also, in your code reject and resolve are not defined. Here's how I would do it,

function getAccountInfo() {
    return new Promise((resolve, reject) => {
        recurly.accounts.list((errResponse, response) => {
            if (errResponse) {
                return reject(errResponse);
            }

            resolve(response);      
        }, );
    });
}

When you call getAccountInfo(), it returns a Promise so you have to wait for it to resolve.

getAccountInfo()
    .then(response => {
        accountInfo = response.data.accounts.account;
        console.log(accountInfo);
    })
    .catch(err => console.log(err));
Rich
  • 423
  • 4
  • 11
  • It worked! Gonna learn more about promise. Thank you very much. – Tian Qin Aug 13 '19 at 22:22
  • Sir I have one more question, so the accountInfo only available inside then scope? What if I want to save the accountInfo to the outter scope that every other function can access it? – Tian Qin Aug 14 '19 at 15:06
  • You can assign accountInfo to a variable outside the scope, my brain won't let me do that. :-) Remember, though, that the outer script has already finished, so using accountInfo has to be done from within that then block. Usually you call another function that does whatever that work is. You can assign accountInfo to a global variable or pass in to your other function. I've had thread safe coding beaten into me, so I always pass things around and never use globals. – Rich Aug 14 '19 at 23:18