Reputation: 3233
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);
Upvotes: 0
Views: 622
Reputation: 994
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)
Upvotes: 1