我正在尝试从我的服务器获取一些数据,并在使用Postman测试请求时,它工作得很好并且我将数据恢复。但是当我尝试在我的应用程序中实现请求时,我回到了一个带有undefined
属性的空对象
CODE:
fetchUserData = (user) => {
axios.post('api', {
username: user
})
.then((res) => {
this.setState({
credit: res.credit,
points: res.points
})
console.log(`this is user stats\ncredit:${res.credit}\npoints:${res.points} for the user: ${user}`)
})
}
router.post('/', (req, res) => {
UserData.findOne({
username: req.body.username,
},
(err, user) => {
if (err) {
console.log('Error in saving user to mongodb: ' + err);
res.send(err);
}
if (user) {
if(user.username === req.body.username) {
console.log('fetching data for: ' + user.username);
console.log(user);
res.send(user);
}
}
});
})
来自邮递员的回应:
{"_id":"5afd9bXXXXXXX664a5af","username":"check","credit":1,"points":0,"__v":0}
对APP控制台的反应:
this is user stats credit:undefined points:undefined for the user: check
答案 0 :(得分:2)
从响应中读取数据我应该使用'res.data.something'而不是res.something