首页 > 解决方案 > 使用 express 将嵌套的 mongodb 文档推送到数组时遇到问题

问题描述

我在将嵌套文档推送到数组时遇到问题。目前我的代码看起来像这样。我知道变量类别是未定义的。我为这个问题感到非常难过,因为我有一次解决了这个问题,但是我的电脑踢了桶,所以我正在处理一个旧的提交。

 db.collection('posts').find().toArray((err, result) => {
    var post = result.filter((post) => {
        return post.id == req.params._id
    })[0];
    var categories = [];
    for(var i=0; i > result.length; i++){
            for(var j=0;j>result[i].postCategories.length;j++){
                categories.push(result[i]postCategories[j]);
            }
        }
    categories = categories.filter( function( item, index, inputArray ) {
       return inputArray.indexOf(item) == index;
});

    res.render('admin/postedit', {
        postTitle: post.postTitle,
        postContent: post.postContent,
        postSlug: post.postSlug,
        categories: categories,
    });
});

标签: javascriptnode.jsmongodbexpress

解决方案


推荐阅读