admin管理员组

文章数量:1315285

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!

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!

Share Improve this question asked Oct 14, 2016 at 19:53 user6898719user6898719 4
  • It sounds like either props or loggingInUser() is undefined, can you try logging out props or debugging and ensuring that both are defined? – Dave V Commented Oct 14, 2016 at 19:55
  • 1 loggingInUser may not be returning anything... – ctrlplusb Commented Oct 14, 2016 at 19:55
  • how does your loggingInUser function look? – StackOverMySoul Commented Oct 14, 2016 at 19:55
  • Possible duplicate of Detecting an undefined object property – Jared Smith Commented Oct 14, 2016 at 21:46
Add a ment  | 

3 Answers 3

Reset to default 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);
    });
}

};

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: {} })
};

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');
}

本文标签: javascriptReactJS What is quotCannot read property 39then39 of undefinedquotStack Overflow