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
orloggingInUser()
is undefined, can you try logging outprops
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
3 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - ReactJS: What is "Cannot read property 'then' of undefined"? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741961931a2407327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论