javascript - 使用 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,
});
});
解决方案
推荐阅读
- batch-file - IF EXIST 'path' 在 FOR /F 中不起作用
- regex - 正则表达式:将字符串拆分为 1 个或多个组
- c - 无法创建函数来创建唯一数组
- github - 如何自动将文件从一个 GitHub 文件夹复制和重命名到另一个
- c++ - 使用条件编译检查用户是否有 SQL Native Client?(ODBC)
- vue.js - 数据达不到怎么办?
- c# - 使用“ApplyConfiguration”时,EF Core 迁移不起作用
- python - 如何从谷歌电子表格中提取数据到笔记本
- javascript - 是否可以将 HTML 代码放入 javascript 中的 innerHTML 中?
- apple-push-notifications - 通过 AirWatch REST API 推送通知?