猫鼬节点js中的'TypeError:meme.find(...).forEach不是函数'? [英] 'TypeError: meme.find(...).forEach is not a function' in mongoose node js?
问题描述
我想遍历MongoDB中的集合,因此我尝试了.forEach
来执行操作,但是看来这不是正确的方法.每次我尝试运行它都会给我错误:TypeError: meme.find(...).forEach is not a function
I want to loop through my collection in MongoDB so I tried .forEach
to perform the action but it looks like this is not the right approach. Everytime I tried running it gives me error: TypeError: meme.find(...).forEach is not a function
这是我的代码:
var meme = require('../app/model/meme');
meme.find().forEach(function(meme){
meme.update({_id: meme._id}, {$set: { objectID: meme._id.toString().slice(10).slice(0,24)}});
});
我正在使用猫鼬和node.js来执行操作.
I'm using mongoose and node.js to perform the action.
希望能帮助您解决此问题.
I'd appreciate any help to solve this problem.
谢谢.
推荐答案
您使用的是异步方法find
,因此您应该使用promise或callback来获取结果,这里有一些解决方案选择您想要的内容
You're using an async method find
so you should use promises or callback to get the result , here some solutions choose what you want
//使用承诺
meme.find().then((memes) => {
memes.forEach((meme) => {
console.log(meme);
});
});
//使用回调
meme.find({}, (err, memes) => {
memes.forEach((meme) => {
console.log(meme);
});
});
//使用exec
meme.find().exec((err, memes) => {
memes.forEach((meme) => {
console.log(meme);
});
});
这篇关于猫鼬节点js中的'TypeError:meme.find(...).forEach不是函数'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!