'TypeError: meme.find(...).forEach 不是一个函数'在猫鼬节点 js 中? [英] '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)}});
});
我使用 mongoose 和 node.js 来执行操作.
I'm using mongoose and node.js to perform the action.
如果您能帮助我解决这个问题,我将不胜感激.
I'd appreciate any help to solve this problem.
谢谢.
推荐答案
您正在使用异步方法 find
所以您应该使用 promises 或 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);
});
});
这篇关于'TypeError: meme.find(...).forEach 不是一个函数'在猫鼬节点 js 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!