【发布时间】:2020-07-07 03:35:49
【问题描述】:
所以,我正在尝试使用获取请求来查找用户的电子邮件并在屏幕上显示信息。为此,我使用用户的电子邮件设置了一个本地存储项,并尝试在我的 api 调用中引用它。但是,在后端方面,当我尝试通过 axios 调用发送电子邮件时,它并没有到达后端,我可以用它做任何事情。我收到了一个状态码 500 错误以及这个 TypeError: Cannot read property 'then' of undefined。
这是我的 router.get 函数,它抛出了错误
router.get("/user", function(req, res) {
console.log("id hit");
console.log(req.data);
console.log(req.params.email);
db.User
.getUserByEmail(req.params.email)
.then((dbModel) => res.json(dbModel))
.catch((err) => res.status(422).json(err));
});
这是我的 getUserByEmail 函数
module.exports.getUserByEmail = function (email, callback) {
console.log("getUserByEmail", email)
var query = { email: email };
console.log(query);
User.findOne(query, callback);
};
这是我的前端 API axios 调用
getUser: function(email) {
console.log("API EMAIL: ",email);
return axios.get("/api/users/user/", email);
}
这是我要显示信息的页面上的 componentDidMount
componentDidMount() {
const userEmail = localStorage.getItem("userEmail");
this.setState({
email: userEmail
}, () => {
console.log(this.state.email);
API.getUser(this.state.email)
.then(data => {
this.setState({
firstName: data.firstName,
lastName: data.lastName,
})
})
})
}
我可以在前端引用电子邮件,但是一旦它到达后端,一切都未定义。任何帮助表示赞赏。
【问题讨论】:
标签: javascript node.js routes axios mern