Answer the question
In order to leave comments, you need to log in
How to output variable from function, nodejs curl replacement?
In general, I've been sitting for three hours and I can't figure it out.
Here is the body variable
var options = {
url: 'https://maps.googleapis.com/maps/api/timezone/json?location=' + latitude + ',' + longitude + '×tamp=1331161200&key=' + googleApiKey,
method: 'GET'
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
{
"dstOffset" : 0,
"rawOffset" : 14400,
"status" : "OK",
"timeZoneId" : "Europe/Moscow",
"timeZoneName" : "Moscow Standard Time"
}
Answer the question
In order to leave comments, you need to log in
What for?
The request is executed asynchronously, access to the body is available only after receiving a response.
function handleBody(body){
console.log(body);
...
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
handleBody(body);
}
});
function myRequest(options){
return new Promise(function(resolve,reject){
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
return resolve(body)
}
//@todo handle errors
reject()
});
});
}
myRequest(options).then(function(body){
//тут теперь продолжается работа с body
}).catch(function(err){
//code
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question