首页 > 解决方案 > 检索用户 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>
        );
};

如果有人可以帮助我,那就太好了!

标签: node.jsmongodbreactjsexpressmongoose

解决方案


我找到了答案。原来它是函数 loadUser (我没有在这里发布)。我忘了id: user.idid: user._id

非常感谢!


推荐阅读