Help needed on passing the response data to another function
See original GitHub issueI am using fetch library as follows… file.js
import {getdata} from 'api.js';
var a = getdata(payload);
console.log(a);
api.js
exports.getdata = (data) => {
fetch(url)
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json);
return json;
}).catch(function(ex) {
console.log('parsing failed', ex)
})
};
variable a is undefined
even though json value exists and it is logged before the parsed json
logging. i am a reactjs newbie and couldn’t figure how to tackle this issue. I am guessing issue is related with asynchronous loading. What am i doing wrong here???
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Passing data to another function - javascript - Stack Overflow
You Should define your variables queryLat and queryLng globally where your script starts. <script type="text/javascript"> var queryLat; var ...
Read more >Ways of passing data to functions in JavaScript
This is done by passing values in parenthesis: myFunction(myData). Even when there is no data to be passed, we still have to declare...
Read more >Pass a function as an argument to another function in JavaScript
https://www.reddit.com/r/learnjavascript/comments/m8vlsb/can_someone_explain_how_to_pass_a_function_as_an/
Read more >How to use promises - Learn web development | MDN
With the fetch() API, once you get a Response object, you need to call another function to get the response data. In this...
Read more >How To Use the JavaScript Fetch API to Get Data - DigitalOcean
Use the json() method to convert response into JSON data: ... Next, you need to set your object and pass it as the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What @dgraham meant is that you are missing a return statement:
Previously, your
getdata
function didn’t have a return statement. Any JavaScript function without a return statement will always returnundefined
, no matter what happens within it.Also, your caller of the
getdata
function should be rewritten like this if you want to actually see the JSON data:Remember that anything that returns a Promise, like
fetch
, you need to always appendthen(...)
to it.I wish you good luck with your JavaScript learning process, but note that this is not a place to ask usage questions whenever you get stuck! Issues for specific projects like this one are for reporting bugs only. You should ask general React or JavaScript question on appropriate forums and Stack Overflow. Thanks!
hello can some one help me to passes data from fetch api response to another page ?