1

I have two method calls within a function called loginUser(). The first passes in userInfo and makes an API request, and then would like to execute the method to empty out the login text fields.

But I am getting the error Cannot read property 'then' of undefined. What may be the issue?

Code:

  _loginUser() {
    this.props.loggingInUser(this.props.userInfo) //Would like to finish this first before moving onto calling this.props.emptyLoginTextFields()
    .then(() => { //Here is where the error is occurring
      this.props.emptyLoginTextFields()
    })
  }

Thank you in advance!

3 Answers3

4

You need to return the the promise manager like "axios" that returns the results ,

example : focus on

"return axios.all"

 getPlayersInfo: function (players) {
    return axios.all(players.map((username) => {
        return getUserInfo(username)

    }))
        .then((info) => {
        return info.map((user) => {
            return user.data;
        });
    })
        .catch( function (err) {
        console.warn("error in getPlayerInfo",err);
    });
}

};

Rbih Zaki
  • 53
  • 7
2

In my case was, that I didn't have an axios mock.

So I created src/__mocks__/axios.js with this code:

export default {
    get: jest.fn().mockResolvedValue({ data: {} })
};
Jöcker
  • 5,281
  • 2
  • 38
  • 44
1

Just be careful to return the promise from the ajax call inside of the loggingInUser function. You probably don't have a return statement in it and the default return value is undefined for JS.

An example:

function loggingInUser(credentials) {
  return fetch('/foo/bar');
}
jannis
  • 4,843
  • 1
  • 23
  • 53
Stefan Turcanu
  • 904
  • 9
  • 13