【问题标题】:Why it says undefined error for existing index为什么它说现有索引的未定义错误
【发布时间】:2020-07-12 07:45:52
【问题描述】:

我已在我的 API 中使用此代码来获取结果集。在这里,它给了我结果集,但是当我尝试访问结果集时,它给了我的undefined

以下是我在 API 中使用的代码

getComments: async(req, res) => 
{
    const { post_id, user_id } = req.body;
    commentSchemaModel.aggregate([{
            "$match": { post_id: post_id }
        },
        { "$sort": { "createdAt": -1 } },
        {
            "$lookup": {
                "from": subcommentSchemaModel.collection.name,
                "localField": "_id",
                "foreignField": "comment_id",
                "as": "subcoms"
            }
        },
        {
            "$project": {
                "_id": 1,
                "user_id": 1,
                "user_name": 1,
                "isUserLiked": 1,
                "comment": 1,
                "user_img": 1,
                "post_id": 1,
                'likes': 1,
                "usersLiked": 1,
                "subcoms": "$subcoms",
                "created": 1,
                "createdAt": 1,
                "updatedAt": 1,
            }
        },
    ]).then(async function(comments) {
        if (comments.length === 0) {
            res.send({ error: true, data: "No Comments" });
        } else {
            comments.forEach((comment) => {
                comment.isUserLiked = comment.usersLiked.some(id => id.equals(user_id))
            });
            
            comments.subcoms.forEach((subcomment) => {
                subcomment.isUserLiked = subcomment.usersLiked.some(id => id.equals(user_id))
            });
            res.send({ error: false, data: comments });
        }
    });
}

它说comments.subcoms 未定义,但 commentssubcomscomments 集合如下所示

[
        {
            "_id": "5f0a8b3e3a95280017d6e985",
            "isUserLiked": false,
            "likes": 0,
            "usersLiked": [],
            "user_id": "5ef9a7a2922eba0017ce47e0",
            "post_id": "5f0a8ab13a95280017d6e97f",
            "comment": "bijjaaa",
            "user_name": "Sudesh",
            "user_img": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1846836948784193&width=400&ext=1596011605&hash=AeRsB0QJQH7edpRT",
            "created": 1594526526162,
            "createdAt": "2020-07-12T04:02:06.164Z",
            "updatedAt": "2020-07-12T04:02:06.164Z",
            "subcoms": [
                {
                    "_id": "5f0ab9a5e8811835342d46fb",
                    "isUserLiked": false,
                    "likes": 0,
                    "usersLiked": [],
                    "user_id": "5ef60bba10e9090017e2c935",
                    "comment_id": "5f0a8b3e3a95280017d6e985",
                    "comment": "ponnaya",
                    "user_name": "Suthura",
                    "user_img": "1594395502410-image_cropper_1594395497877.jpg",
                    "imagesource": "userimage",
                    "created": 1594538405044,
                    "createdAt": "2020-07-12T07:20:05.050Z",
                    "updatedAt": "2020-07-12T07:20:05.050Z",
                    "__v": 0
                }
            ]
        }
    ]

这可能是什么错误,我该如何解决?

【问题讨论】:

  • 第一个索引处的对象内部没有comment.subcomssubcoms。在第二个 forEach 中使用 subcomment.subcoms
  • cmets 是一个数组,该数组下的对象包含子coms,因此您不能通过 cmets 访问它,而是可以在forEach 内部尝试并尝试使用comment.subcoms
  • @KarthikRadhakrishnan 工作正常。感谢帮助。我会发布答案

标签: javascript node.js arrays mongodb mongoose


【解决方案1】:

你在这里迭代comments

comments.forEach((subcomment) => {
                //subcomment.subcoms is accessible
                subcomment.isUserLiked = subcomment.usersLiked.some(id => id.equals(user_id))
            });

//here first index of comments and array inside subcoms accessible

comments[0]['subcoms'].forEach((subcomment) => {
                subcomment.isUserLiked = subcomment.usersLiked.some(id => id.equals(user_id))
            });

【讨论】:

    【解决方案2】:

    根据@KarthikRadhakrishnan 的说法,嵌套循环做了我想要的。

     comments.forEach((comment) => {
                        comment.isUserLiked = comment.usersLiked.some(id => id.equals(user_id))
                        comment.subcoms.forEach((subcomment) => {
                            subcomment.isUserLiked = subcomment.usersLiked.some(id => id.equals(user_id))
                        });
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多