为什么我不能在我的 id 前端将它 post 传递给后端?

Why can't I post in my id frontend when passing it to backend?

我遇到了一个问题,我想 post 我的项目,但它不起作用..这是前端的代码,但我会先解释一下..所以基本上是这里的过程是当我注册我的帐户时,它会自动添加到我的用户和我的猫鼬中的喜欢者模型。更具体地说..我有这个东西

  useEffect(() => {
    
      axios.get('http://localhost:7171/users/')
        .then(list => setlist(list.data))
        .catch(err => console.log(err))
        // console.log(list)

      axios.get('http://localhost:7171/likes')
        .then(listid => setlistid(listid.data))
        .catch(err => console.log(err))

  },[])

它会读取我创建的数据...然后当我注册我的项目时它会执行类似这样的操作...

const Register = e => {

    axios.post('http://localhost:7171/users/register',e)
      .then(res => console.log(res))
      .catch(err => console.log(err))

}

//Something function submit here 
if (name.length >= 6 && key.length >= 6) {
          setCallError("Your Account has Registered")
          Register(Inputs)   
          console.log(listid[0]._id,listid[1]._id,listid[2]._id,listid[3]._id,listid[4]._id)

          axios.post(`http://localhost:7171/likes/item/${listid[0]._id}`,{ people:{name:name,key:key }})
            .then(res => console.log(res.data))
            .catch(err => console.log('Error:' + err))

          // axios.post('http://localhost:7171/likes/item/'+listid[1]._id,{ people:{name:name,key:key }})
          //   .then(res => console.log(res.data))
          //   .catch(err => console.log('Error:' + err))

          // axios.post('http://localhost:7171/likes/item/'+listid[2]._id,{ people:{name:name,key:key }})
          //   .then(res => console.log(res.data))
          //   .catch(err => console.log('Error:' + err))

          // axios.post('http://localhost:7171/likes/item/'+listid[3]._id,{ people:{name:name,key:key }})
          //   .then(res => console.log(res.data))
          //   .catch(err => console.log('Error:' + err))

          // axios.post('http://localhost:7171/likes/item/'+listid[4]._id,{ people:{name:name,key:key }})
          //   .then(res => console.log(res.data))
          //   .catch(err => console.log('Error:' + err))

        }

不要介意其他代码功能,这只是向您展示它会处理的方式,只关注这件事

axios.post(`http://localhost:7171/likes/item/${listid[0]._id}`,{ people:{name:name,key:key }})
            .then(res => console.log(res.data))
            .catch(err => console.log('Error:' + err))

我只是先注释掉我的另一个 post 这样你就可以确定我在这里做什么..因为我只想 post 我的 5 个项目在同一个项目..但是这是一个问题。当我尝试提交它时,它会在我的 post POST http://localhost:7171/likes/item/00000001a24dfc9b806e607f 400 (Bad Request) 中出现这种错误 我不明白为什么它是错误的 我只是在 id 中传递它..顺便说一句,这是我的后端优先

此代码表示我想重新更新我的特定 ID,因为我想在其列表中添加一个新项目 people每当我注册一个用户。

router.route('/item/:id').post((req,res) => {

    const { likes, people } = req.body
    
    const id = req.params.id
    
    console.log(id)

    if (!id) return res.status(400).json({ message: "missing id" });

    Main.updateOne(
        { _id: (id) },
        {
            $set: {
                likes,
            },
            $addToSet: { people: { $each: people } },
        },
        )
        .then((likes) => res.json({ message: "New User Added" }))
        .catch((err) => res.status(400).json("Error :" + err));


})  

但我在这里的意思是我传递了我在数据中读取的正确 ID,但我不明白什么时候我试图在读取数据的 ID 中 post 它。如您所见,listid[0]._id 这意味着我想传递我创建的 ID,但我不明白为什么它不能 post 项目...但是当我尝试在中创建它时我的 post 男人就是这样。它工作得很好..你们有什么想法吗?或者注意到我的代码有误,我在任何地方都看不到它。

已编辑 这是 console.log(listid)

这是后端部分

错误说 $each 接受一个数组作为参数,但是 people 在从客户端发出请求时似乎没有被定义。您应该像在 Postman 中一样将人员添加到 Axios 请求正文中。或者,如果 people 未定义,您可以传递一个空数组:

Main.updateOne({
    _id: (id)
  }, {
    $set: {
      likes,
    },
    $addToSet: {
      people: {
        $each: people || [] // empty array if people is undefined
      }
    },
  }, )
  .then((likes) => res.json({
    message: "New User Added"
  }))