我在 mongoDB 中创建了一个简单的表(文档?)。 我正在使用 Node 和 Mongoose 连接到它。
在我的方法中,我调用 model.find({})
来检索所有记录,然后迭代它们以找到我想要的记录(这是在一个循环内 - 我在想命中一次DB,然后在内存中处理会更高效,避免每次都连接数据库)。
当我 console.log
比赛时,我会打印出完整的对象。然而,当我打印出一个属性时,它会将其列为未定义。该属性是一个数组,并且另一个具有我作为测试添加的数组的属性也发生了这种情况。我在这里缺少什么?
这是我的代码片段:
Documents.find({}).then(docsData => { // Documents is my model
docs.entries.forEach(entry => { // docs.entries is the collection I want to match to
const match = docsData.find(
doc => doc['dropboxId'] == entry['id']
);
if (match) {
entry['tags'] = match.tags;
console.log('match tags', match.tags); // this prints out undefined
console.log('match', match); // this prints out the object with tags
}
有什么想法吗?
最佳答案
match
是一个 Mongoose 文档,与普通的 JS 对象不同。我想你需要这样做:
entry['tags'] = match.get('tags');
关于javascript - 对数组的 mongoose.model 进行索引返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904506/