0

I am creating a login script that should return the responseData back to the calling class. The problem is that I am constantly getting undefined with the returned result. If I console log the responseData it shows up fine. One thing I have noticed is that the undefined sometimes appears before the console log responseData in the console.

module.exports = {

    loginUser: function( emailAddress = "", password = "" ) {

        var url= "URL_HERE";

        console.log(url);

        fetch(url, {method: "GET"})
            .then((response) => response.json())
            .then((responseData) =>
        {   

            console.log(responseData);
            return responseData;


        })
        .done(() => {

        });

    },

    otherMethod: function() {}

}

Here is an example of how the function is called

var dataLogin = require('../../data/login.js');
var result = dataLogin.loginUser(this.state.formInputEmail, this.state.formInputPassword);
ORStudios
  • 3,157
  • 9
  • 41
  • 69
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mayank Shukla May 31 '17 at 09:38
  • 1
    pass a callback method to loginUser method and call the method when you get the response. – Mayank Shukla May 31 '17 at 09:39

1 Answers1

1

I suspect you mean the return value of loginUser(). It doesnt return anything. It's the inner lambda passed to .then() that returns something.

Try returning the entire fetch-promise from loginUser(), and then use loginUser as a promise:

...
return fetch( ...
...
loginUser().then(responseData => useResponseDataForSomething(responseData)
jonas
  • 984
  • 10
  • 15