question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Help needed on passing the response data to another function

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
mislavcommented, Nov 5, 2016

What @dgraham meant is that you are missing a return statement:

exports.getdata = (data) => {
   return fetch(url)...
// ^^^ this return statement right here

Previously, your getdata function didn’t have a return statement. Any JavaScript function without a return statement will always return undefined, 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:

var a = getdata(payload)
a.then((result) => console.log(result))

Remember that anything that returns a Promise, like fetch, you need to always append then(...) 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!

0reactions
SamuelALSNcommented, Sep 5, 2019

hello can some one help me to passes data from fetch api response to another page ?

$(document).on('click', '#edit-property', function () {
        var data = $(this).data('info');
        fetch('/property/'+data)
            .then(response => {
            if (response.ok) {
                response.json().then(property => {
                    console.log(property);
                    window.location.href="/property-details";

                })
            } else {
                console.error(' Reponse serveur : ' + response.status);
            }

        });
    });
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found