1

When trying to do a HTTP request in a lambda function, the following GET does not seem to be executed:

 var request = require("request"); var myMonzoBalance; request({ uri: "https://api.monzo.com/balance?account_id=acc_XXXXXXXXX", method: "GET", headers: {'Authorization': 'Bearer XXXXXXX'} }, function(error, response, body) { myMonzoBalance = JSON.parse(body).balance; console.log(myMonzoBalance); }); console.log(myMonzoBalance); 

The value for myMonzoBalance will be undefined once the code is executed.

Actually if I try to give myMonzoBalance a value inside the request function, it will not make any difference - undefined.

The above code works fine when run in Terminal. Also I have node_modules within the library of that same lambda function.

Any ideas about why this could be happening?

Many thanks!

1
  • In the callback function function(error,response,body), could you post the result of "console.log(body)", so we are sure that "JSON.parse(body).balance" is an object that actually exists? Commented Nov 11, 2016 at 22:30

1 Answer 1

0

This is the order of execution:

1 var request = require("request"); 2 var myMonzoBalance; 3 request({ uri: "https://api.monzo.com/balance?account_id=acc_XXXXXXXXX", method: "GET", headers: {'Authorization': 'Bearer XXXXXXX'} }, function(error, response, body) { 5 myMonzoBalance = JSON.parse(body).balance; 6 console.log(myMonzoBalance); }); 4 console.log(myMonzoBalance); 

It is not the case that it is undefined outside of the function. It's just not defined yet when you run the final console.log().

Of course it's also possible that you don't get any response from the request (you cat test it by running console.log(body) instead of console.log(myMonzoBalance) in the callback function) but even if you get the response and it has a balance field then this line myMonzoBalance = JSON.parse(body).balance would still run after the value is printed in the last line of your code.

First step of fixing the problem would be to change the code to make sure that you get the right response:

var request = require("request"); var myMonzoBalance; request({ uri: "https://api.monzo.com/balance?account_id=acc_XXXXXXXXX", method: "GET", headers: {'Authorization': 'Bearer XXXXXXX'} }, function(error, response, body) { if (error) { console.log('ERROR:', error); } else { console.log('BODY:', body); console.log('BALANCE:', JSON.parse(body).balance); } }); 

and only if it prints what you need try to fix the timing issue.

To get a better understanding on the asynchronous nature of Node that is giving this execution order here, see those answers:

This is the most common thing in Node that people confuse and I've written a lot of answers about it which you may also find helpful.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your prompt reply.If I remove the last console.log(), it shows as undefined as well...
@user3302071 What is shown as undefined? ERROR, BODY or BALANCE?
All of them... I don´t think the function is executed at all... If I try from Terminal, I get a value for BODY and BALANCE...
@user3302071 It can't be all of them. That function will print either the ERROR or the BODY and BALANCE if it is run. It doesn't set any value, it just prints ERROR or BODY/BALANCE to see if the request works at all. If it works then you can add myMonzoBalance = JSON.parse(body).balance; but keep in mind that you can't access it before it was set. See the execution order that I described in my answer by adding numbers to the source code. This is a crucial thing to understand here - the order in which the code gets executed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.