首页 > 解决方案 > 如何在 mongoose 中将此数组传递给回调 'meme.find({}, (err, meme) => { const memes = meme.map(m => m); });'

问题描述

我得到了将以下猫鼬查询中的数组传递给回调的任务,但是作为初学者,我不知道如何正确执行此操作。

meme.find({}, (err, meme) => {
  const memes = meme.map(m => m);
  // Use the array, pass it to a service, or pass to a callback
});

const memes = meme.map(m => m);是问题中的数组。

拜托,有人可以阐明我如何将它传递给回调吗?

谢谢。

标签: javascriptnode.jsmongodbmongoose

解决方案


您可以将其包装在匿名函数中并传递该数组。然后在任何你想要的地方使用那个数组

(function (memes){
meme.find({}, (err, meme) => {

  // You can access memes array here
});
}(memes));

推荐阅读