首页 > 解决方案 > 如何解决 mongodb mongoose 中的“TypeError: db.find(...).toArray is not a function”?

问题描述

我正在尝试查询我的 mongodb 集合以更新字段,但在尝试使用.toArray循环所有文档时遇到错误。

每当我运行下面的代码时,它都会显示TypeError: meme.find(...).toArray is not a function.

我做错了什么,我能做些什么来解决它?

var mongoose = require('mongoose');
const mongoolia = require('mongoolia').default;
var algoliasearch = require('algoliasearch');

//meme schema
var meme = require('../app/model/meme');

const TagSchema = new mongoose.Schema({
  tagarray: { type: String, required: true, algoliaIndex: true },
});

TagSchema.plugin(mongoolia, {
  appId: 'QQVL4GOPOL',
  apiKey: 'd93766f242500de7a13183eeeb6a7934',
  indexName: 'test1'
})

meme.find().toArray(function(memes){
   meme.update({_id: memes._id}, {$set: { objectID: memes._id.toString().slice(10).slice(0,24)}}); 
}); 

标签: node.jsmongodbmongoose

解决方案


更改find().toArray()find().fetch();


推荐阅读