Saving body from request.get() [duplicate]Why is my variable unaltered after I modify it inside of a function? - Asynchronous code referenceHow do I return the response from an asynchronous call?How do I remove a property from a JavaScript object?Remove empty elements from an array in JavascriptGet selected text from a drop-down list (select box) using jQueryHow do I remove a particular element from an array in JavaScript?How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Writing a JSON file after a lot of async requestsHow do I return the response from an asynchronous call?What is the --save option for npm install?Request data to global variableWait for data from external API before making POST request
Solenoid fastest possible release - for how long should reversed polarity be applied?
"Counterexample" for the Inverse function theorem
Polynomial division: Is this trick obvious?
Would a "ring language" be possible?
How can I fix the label locations on my tikzcd diagram?
How can we delete item permanently without storing in Recycle Bin?
What formula to chose a nonlinear formula?
Given 0s on Assignments with suspected and dismissed cheating?
Do we see some Unsullied doing this in S08E05?
Promotion comes with unexpected 24/7/365 on-call
Cannot remove door knob -- totally inaccessible!
Would it be fair to use 1d30 (instead of rolling 2d20 and taking the higher die) for advantage rolls?
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5
How could it be that 80% of townspeople were farmers during the Edo period in Japan?
Why would you put your input amplifier in front of your filtering for and ECG signal?
Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?
What would a Dragon have to exhale to cause rain?
How was the blinking terminal cursor invented?
When the match time is called, does the current turn end immediately?
How does the Heat Metal spell interact with a follow-up Frostbite spell?
When did Britain learn about American independence?
Why is so much ransomware breakable?
Failing students when it might cause them economic ruin
Saving body from request.get() [duplicate]
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code referenceHow do I return the response from an asynchronous call?How do I remove a property from a JavaScript object?Remove empty elements from an array in JavascriptGet selected text from a drop-down list (select box) using jQueryHow do I remove a particular element from an array in JavaScript?How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Writing a JSON file after a lot of async requestsHow do I return the response from an asynchronous call?What is the --save option for npm install?Request data to global variableWait for data from external API before making POST request
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
I want to save the body from a get request in a variable (using the request module), but its undefined.
I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).
const request = require("request")
...
let foo;
request.get(options,function(err,response,body)
foo = body;
);
console.log(foo);
expected: the body of the valid get-request
actual: undefined
javascript node.js node-request
marked as duplicate by Jonathan Lonowski
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 23 at 16:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
I want to save the body from a get request in a variable (using the request module), but its undefined.
I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).
const request = require("request")
...
let foo;
request.get(options,function(err,response,body)
foo = body;
);
console.log(foo);
expected: the body of the valid get-request
actual: undefined
javascript node.js node-request
marked as duplicate by Jonathan Lonowski
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 23 at 16:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
I want to save the body from a get request in a variable (using the request module), but its undefined.
I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).
const request = require("request")
...
let foo;
request.get(options,function(err,response,body)
foo = body;
);
console.log(foo);
expected: the body of the valid get-request
actual: undefined
javascript node.js node-request
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
I want to save the body from a get request in a variable (using the request module), but its undefined.
I've searched for a solution but nothing worked so far. Guess the problem is that request.get is calles async (so far i have no experience in async functions).
const request = require("request")
...
let foo;
request.get(options,function(err,response,body)
foo = body;
);
console.log(foo);
expected: the body of the valid get-request
actual: undefined
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
javascript node.js node-request
javascript node.js node-request
asked Mar 23 at 16:37
DrSteifDrSteif
1
1
marked as duplicate by Jonathan Lonowski
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 23 at 16:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jonathan Lonowski
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 23 at 16:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The request is done in asynchronous way so your console.log
happens before the request is completed, so foo is still undefined.
You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.
request.get(options,function(err,response,body)
console.log(body);
doSomethingAfterMyRequest(body)
);
Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.
request.get(options,function(err,response,body)
if (err)
console.error(err)
return
console.log(body);
doSomethingAfterMyRequest(body)
);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The request is done in asynchronous way so your console.log
happens before the request is completed, so foo is still undefined.
You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.
request.get(options,function(err,response,body)
console.log(body);
doSomethingAfterMyRequest(body)
);
Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.
request.get(options,function(err,response,body)
if (err)
console.error(err)
return
console.log(body);
doSomethingAfterMyRequest(body)
);
add a comment |
The request is done in asynchronous way so your console.log
happens before the request is completed, so foo is still undefined.
You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.
request.get(options,function(err,response,body)
console.log(body);
doSomethingAfterMyRequest(body)
);
Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.
request.get(options,function(err,response,body)
if (err)
console.error(err)
return
console.log(body);
doSomethingAfterMyRequest(body)
);
add a comment |
The request is done in asynchronous way so your console.log
happens before the request is completed, so foo is still undefined.
You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.
request.get(options,function(err,response,body)
console.log(body);
doSomethingAfterMyRequest(body)
);
Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.
request.get(options,function(err,response,body)
if (err)
console.error(err)
return
console.log(body);
doSomethingAfterMyRequest(body)
);
The request is done in asynchronous way so your console.log
happens before the request is completed, so foo is still undefined.
You must put your console log inside the callback body. Also, any computation requiring the result from the request should be started from within the callback.
request.get(options,function(err,response,body)
console.log(body);
doSomethingAfterMyRequest(body)
);
Also, you should handle your error, because if an error occurs during your request, it's possible that body remains undefined.
request.get(options,function(err,response,body)
if (err)
console.error(err)
return
console.log(body);
doSomethingAfterMyRequest(body)
);
answered Mar 23 at 16:44
François P.François P.
2,115716
2,115716
add a comment |
add a comment |