为什么我的添加好友测试路由在 insomina 中通过了 200 OK,但是当我调用获取所有用户时却没有嵌套?

Why my add friend testing route goes through in insomina with 200 OK, but it's not nested when I call to get all users?

我正在使用 mondoDB 和 moongose NoSQL。当我尝试向用户添加朋友时,我有这个代码。

 addFriend({ params }, res) {
   User.updateOne
   ({ _id: params.friendId })
     .then(dbUserData => res.json(dbUserData))
     .catch(err => res.json(err));
 },

http://localhost:3001/api/users/61286c3285a0afa77a1b869e/friends/6128178de88611885576fdd6

那是测试路线,我明白了:

{
  "driver": true,
  "name": "MongoError",
  "index": 0,
  "code": 66
}

目标是将其添加到主路由中:

http://localhost:3001/api/users

这是我得到的:

 {
    "_id": "61286c3285a0afa77a1b869e",
    "username": "testing",
    "email": "testing@gmail.com",
    "friends": []
  },

目标是将其嵌套到朋友的数组中。

要更新文档,您必须查询特定文档 userId 并将文档 push friendId 更新为 friend 字段

addFriend({ params }, res) {
   User.updateOne
   ({ _id: params.userId },{$push:{friends:params.friendId})
     .then(dbUserData => res.json(dbUserData))
     .catch(err => res.json(err));
 },