Counter J
Counter J

Reputation: 183

How to solve 'TypeError: db.find(...).toArray is not a function' in mongodb mongoose?

I'm trying to query my mongodb collection so as to update a field but am facing an error when trying to use .toArray to loop through all the documents.

Whenever I run the code below, it shows TypeError: meme.find(...).toArray is not a function.

What am I doing wrong and what can I do to solve it?

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

Upvotes: 2

Views: 8976

Answers (2)

Ali Najafi
Ali Najafi

Reputation: 143

i found this after a lot of try

 db.collection(collection).find(data, (err, res) => err ? reject(err) : res.toArray().then(x => resolve(x)))

Upvotes: 0

Robin Jiao
Robin Jiao

Reputation: 113

change find().toArray() to find().fetch();

Upvotes: 1

Related Questions