node.js - 检索用户 ID 时出错
问题描述
我的应用程序基本上可以让您放置照片的链接并检测人的面部。所以这是交易,我正在与 MERN 一起开发它,一切正常,但这部分:
app.put('/image', (req, res) => {
const { id } = req.body;
User.findByIdAndUpdate(id, {$inc : { 'entries': 1}})
console.log(id);
.then(user => {
return res.json(user.entries);
})
.catch(err => {
res.status(400).json('Error getting entries');
console.log(err);
})
});
当一个控制台记录它时,它基本上说'id'因此'user'是未定义的。当我尝试使用 Postman 直接发送 ID 时,它工作正常,但我想不出它为什么返回未定义。我猜这是前端的东西没有获取用户的 ID 并发送到后端。
这就是 onSubmit 函数:
onButtonSubmit = () => {
this.setState({imageUrl: this.state.input});
app.models
.predict(
Clarifai.FACE_DETECT_MODEL,
this.state.input)
.then(response => {
if (response) {
fetch('http://localhost:3000/image', {
method: 'put',
headers: {'Content-Type': 'application/json' },
body: JSON.stringify({
id: this.state.user.id
})
})
.then(response => response.json())
.then(count => {
this.setState(this.setState(Object.assign(this.state.user, {entries: count})))
})
}
this.displayFaceBox(this.calculateFaceLocation(response))
})
}
这就是 Rank 组件,它应该显示一个有多少条目。
const Rank = ({ name, entries }) => {
return (
<div>
<div className='f3'>
{`${name} , your current entry count is...`}
</div>
<div className='white f1'>
{entries}
</div>
</div>
);
};
如果有人可以帮助我,那就太好了!
解决方案
我找到了答案。原来它是函数 loadUser (我没有在这里发布)。我忘了id: user.id
换id: user._id
非常感谢!
推荐阅读
- python - python keras - 预测时间序列,基于相似序列的历史样本很少
- javascript - 使用带有 Promise.all() 的配置对象
- android - 其他类中的Android Dagger 2 Activity上下文
- hl7-fhir - 在单个资源中插入多个观察值 - FHIR
- python - 如何使用 pyqt5 在一个主文件中连接 3 个不同的程序?
- python - 为什么 keras 层中的 L2 归一化会扩大暗淡?
- ef-core-3.1 - 在 EF Core 中按多个子值对实体进行排序
- android - 避免sqlite数据库中重复的游标函数
- java - @SerializedName 不反映在子类中
- c++ - 第一个唯一编号慢运行时问题