javascript - res.json not working inside callback function -


i'm working on api endpoint.

upon posting data endpoint /authenticate, use plaidclient.getauthuser function user's account information, , i'm trying use res.json return account data.

after running this:

accounts = json.stringify(res.accounts); console.log('accounts: ' + accounts);

i able see array of dictionaries containing account information.

however, when attempt use res.json({accounts: accounts}), error:

res.json({accounts: accounts}); ^ typeerror: undefined not function

when try run res.send(accounts) in place of res.json, receive same error:

res.send({accounts: accounts}); ^ typeerror: undefined not function

here's code:

var public_token = ""; var access_token = ""; var accounts = [];

 app.post('/authenticate', function (req, res) {     console.log('post');     public_token = req.body.public_token;     console.log(public_token);     console.log('plaid: ' + app.client);     server.client.exchangetoken(public_token, function (err, res) {         console.log('exchange');         if (err) {             //handle error             console.log('exchange-error');         }         else {             //we have exchanged our plaid access token.             //store somewhere persistent             //access token used make api calls             access_token = res.access_token;             console.log('access_token: ' + access_token);              server.client.getauthuser(access_token, function (err, res) {                 if (err != null) {                     //handle error                     console.log('authorization-error');                 }                 else {                     // our response hold array of accounts user, containing account names,                     // balances, account , routing numbers.                     accounts = json.stringify(res.accounts);                     console.log('accounts: ' + accounts);                      //return account data                     res.json({accounts: accounts});                 }             });         }     });      res.contenttype('application/json');      res.send(accounts); }); 

};`

also, when run final 2 lines:

res.contenttype('application/json'); res.send(accounts);

the account variable defaults it's initial value of []

i think may closure issue i'm not sure.

 app.post('/authenticate', function (req, res) {     console.log('post');     public_token = req.body.public_token;     console.log(public_token);     console.log('plaid: ' + app.client);     server.client.exchangetoken(public_token, function (err, ***res***) { 

you have overwritten res in second callback


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -