[英]Indexing into mongoose.model for an array is returning undefined
我在mongoDB中创建了一个简单的表(文档?)。 我正在使用Node和Mongoose连接到它。
在我的方法中,我正在调用model.find({})
来检索所有记录,然后遍历它们以查找所需的记录(这在循环内-我认为一次访问数据库将更有效,然后在内存中进行处理以避免每次都连接到数据库)。
当我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
是与常规JS对象不同的Mongoose文档。 我想您需要这样做:
entry['tags'] = match.get('tags');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.