Node.js - Global Variable retiring null even though it is assigned

321 views Asked by At

When using Plaid, I use app.post to set the access token, then when I use app.get to retrieve the identity, I receive a null value for the global variable even when it is assigned. Sometimes it returns a null value, other times it returns the access token. The code is currently running server side on a Node server but every time I run it locally, I don't have this issue.

var ACCESS_TOKEN = null;

app.post('/api/set_access_token', function(request, response, next) {
  PUBLIC_TOKEN = request.body.public_token;
  client.exchangePublicToken(PUBLIC_TOKEN, function(error, tokenResponse) {
    if (error != null) {
      prettyPrintResponse(error);
      return response.json({
        error: error,
      });
    }
    ACCESS_TOKEN = tokenResponse.access_token;
   
    ITEM_ID = tokenResponse.item_id;
    console.log("Access Token: "+ ACCESS_TOKEN);
    prettyPrintResponse(tokenResponse);
    response.json({
      
      item_id: ITEM_ID,
      error: null,
    });
  });
});



app.get('/api/auth', function(request, response, next) {
  console.log("Access Token: "+ ACCESS_TOKEN);

  });

Here are the server logs:

PlaidError: INVALID_FIELD
    at rejectWithPlaidError (/usr/src/app/data/cloud/node_modules/plaid/lib/plaidRequest.js:18:19)
    at /usr/src/app/data/cloud/node_modules/plaid/lib/plaidRequest.js:101:18
    at processTicksAndRejections (internal/process/task_queues.js:89:5) {
  name: 'PlaidError',
  display_message: null,
  documentation_url: 'https://plaid.com/docs/?ref=error#invalid-request-errors',
  error_code: 'INVALID_FIELD',
  error_message: 'access_token must be a non-empty string',
  error_type: 'INVALID_REQUEST',
  request_id: 'some_request_id',
  suggested_action: null,
  status_code: 400
}

[2020-10-15T19:03:17.732Z]
Access Token: null

[2020-10-15T19:00:12.150Z]
Access Token: access-sandbox-token

[2020-10-15T19:00:09.201Z]
{
  access_token: 'access-sandbox-token',
  item_id: 'some_item_id',
  request_id: 'some_request_id',
  status_code: 200
}
0

There are 0 answers