最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'TypeError: meme.find(...).forEach is not a function' in mongoose node js? - Stack Overflow

programmeradmin1浏览0评论

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

This is my code:

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)}}); 
});

I'm using mongoose and node.js to perform the action.

I'd appreciate any help to solve this problem.

Thanks.

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

This is my code:

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)}}); 
});

I'm using mongoose and node.js to perform the action.

I'd appreciate any help to solve this problem.

Thanks.

Share Improve this question asked Jun 24, 2018 at 12:11 Counter JCounter J 1831 gold badge4 silver badges17 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

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);
  });
});

You should try the following:

const f = async () => {
  for await (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.

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
});
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.

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
  });
发布评论

评论列表(0)

  1. 暂无评论