'typeerror: Meme.find(...).foreach Is Not A Function' In Mongoose Node Js?
Solution 1:
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
// using promises
meme.find().then((memes) => {
memes.forEach((meme) => {
console.log(meme);
});
});
// using callbacks
meme.find({}, (err, memes) => {
memes.forEach((meme) => {
console.log(meme);
});
});
// using exec
meme.find().exec((err, memes) => {
memes.forEach((meme) => {
console.log(meme);
});
});
Solution 2:
You should try the following:
const f = async () => {
forawait (let m of meme.find()) {
// do something with m
}
}
mongoose "find" function returns query, which is iterable. So, no need to get all document at once.
Solution 3:
find
method is async function so it doesn't return results but instead you need to pass a callback
Here you have documentation of that method
So you should make a call eg. like this:
meme.find().exec(function (err, docs) {
// something
});
Solution 4:
var meme = require('../app/model/meme');
meme.find().then(memes => {
memes.forEach(function(meme){
meme.update({_id: meme._id}, {$set: { objectID: meme._id.toString().slice(10).slice(0,24)}});
});
})
try above one, mongoose return a promise, you have to first execute the results after that you can iterate over that results.
Solution 5:
There are three main state in the function()
You need to update the data if you find the data in stream then need to handle if something went wrong... and need to handle final that function has done it's work need to give call back
no data found...
var meme = require('../app/model/meme');
meme.find(query).stream()
.on('data', function(meme){
meme.update({_id: meme._id}, {$set: { objectID: meme._id.toString().slice(10).slice(0,24)}});
})
.on('error', function(err){
//Getting `error` Something went wrong
})
.on('end', function(){
// finally end
});
Post a Comment for "'typeerror: Meme.find(...).foreach Is Not A Function' In Mongoose Node Js?"