首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >推入阵列猫鼬和猫鼬

推入阵列猫鼬和猫鼬
EN

Stack Overflow用户
提问于 2019-06-06 04:19:42
回答 1查看 35关注 0票数 0

每当我试图插入嵌套在注释中的likeList数组时,我都会得到以下错误likeList

在执行下列操作时:

代码语言:javascript
复制
   Feed.findOneAndUpdate(
          {
            owner: req.body.authorId,
            "posts.comments.commentList._id": req.body.commentId
          },
          {
            $push: {
              "posts.$.comments.commentList.likes.likeList": {
                user: req.user._id,
                avatar: req.user.profile.profile_picture.url,
                name: req.user.name
              }
        )

我的模式如下:

馈电模式

代码语言:javascript
复制
 owner: {
    type: Schema.Types.ObjectId,
    ref: "userType"
  },
         posts: [
            {
               author: {
               userType: {
               type: String,
               enum: ["IndustryPartner", "User", "School"]
            },
            user: {
              type: Schema.Types.ObjectId,
              ref: "posts.author.userType", //<- This may cause an issue, if there are any issues with retrieving user fields, CHECK THIS
              required: true
            },
            name: { type: String, required: true },
            avatar: { type: String, required: true }
          },
              comments: {
                totalComments: { type: Number, default: 0 },
                commentList: [
                  {
                    likes: {
                      totalLikes: { type: Number, default: 0 },
                      likeList: [ <---//Trying to push here
                        {
                          user: { type: Schema.Types.ObjectId, ref: "User" },
                          avatar: { type: String },
                          name: { type: String },
                          date: {
                            type: Date,
                            default: Date.now
                          }
                        }
                      ]
                    ...

我不确定这是否是我在第一个用于筛选的参数中使用的查询的问题。

更新整个错误消息这是奇怪的,因为它看起来实际上正在找到正确的commentList,但无法访问数组本身中的commentList字段。我是否错了,假设这应该能够通过它?posts.$.comments.commentList.likes.likeList

代码语言:javascript
复制
      { MongoError: Cannot create field 'likes' in element {commentList: [ { likes: { totalLikes: 0, likeList: [] }, 
_id: ObjectId('5cf6b3293b61fe06f48794e3'), user: ObjectId('5c9bf6eb1da18b038ca660b8'), avatar: "https://sli.blob.core.windows.net/stuli/
profile-picture-e1367a7a-41c2-4ab4-9cb5-621d2008260f.jpg", name: "Luke Skywalker", text: "Test comment from Luke", repliesToComment: [], date: new Date(1559671593009) } ]}
EN

回答 1

Stack Overflow用户

发布于 2019-06-06 16:35:56

经过进一步的研究,经过两个层次的数组后,位置算子似乎不再有用。因此,解决方案是使用JS更改将值推送到数组中,然后保存它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56470842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档