5

I am creating a database with MongoDB and using the Mongoose ODM. I'm using Node.js. I ran the code without the last block several times and it was fine, but when I wrote the last block in order to use the .find() method, it threw me an odd error.

This is the app.js file:

//require mongoose
const mongoose = require('mongoose');
//connect to mongoDB database
mongoose.connect('mongodb://localhost:27017/albumDB', {useNewUrlParser: true, useUnifiedTopology: true});

//CREATE

//create schema (blueprint/structure)
//of data that we save to the database
const albumSchema = new mongoose.Schema ({
  name: String, //the DB has a variable called name with a value of String
  author: String,
  year: Number,
  genre: String,
  listened: Boolean,
  liked: Boolean
});


//creating the model. parameters: object of collection, schema
const Album = mongoose.model('Album', albumSchema);
//creating the album document
const album = new Album({
  name: 'Insurgentes',
  author: 'Steven Wilson',
  year: 2008,
  genre: 'Prog rock',
  listened: 1,
  liked: 1
});
//save album inside Album inside albumDB
//album.save().then(() => console.log('meow'));


const personSchema = new mongoose.Schema ({
  name: String,
  age: Number
});

const Person = mongoose.model('Person', personSchema);

const person = new Person({
  name: "John",
  age: 37
});

//person.save();

const SiameseDream = new Album({
  name: 'Siamese Dream',
  author: 'The Smashing Pumpkins',
  year: 1993,
  genre: 'Alt rock, Grunge',
  listened: 1,
  liked: 1
});

const MellonCollie = new Album({
  name: 'Mellon Collie and the Infinite Sadness',
  author: 'The Smashing Pumpkins',
  year: 1995,
  genre: 'Alt rock, Dream pop',
  listened: 1,
  liked: 1
});

const Adore = new Album({
  name: 'Adore',
  author: 'The Smashing Pumpkins',
  year: 1998,
  genre: 'Alt rock, Art rock',
  listened: 1,
  liked: 1
});    

//READ

Album.find(function (err, albums){ //1. error 2.what it finds back
  if (err) {
    console.log(err);
  } else {
  console.log(albums);
  }
});

This is the error that shows up on my terminal, related to the last block of code:

$ node app.js
TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\query.js:2151:19)    
    at model.Query._wrappedThunk [as _find] (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
    at C:\Users\user\Desktop\Music\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (internal/process/task_queues.js:75:11)
(node:11536) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
    at Collection.find (C:\Users\user\Desktop\Music\node_modules\mongodb\lib\collection.js:238:19)
    at NativeCollection.<computed> [as find] (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
    at NativeCollection.Collection.doQueue (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\collection.js:135:23)
    at C:\Users\user\Desktop\Music\node_modules\mongoose\lib\collection.js:82:24
    at processTicksAndRejections (internal/process/task_queues.js:75:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11536) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)(node:11536) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

3 Answers 3

4

This issue is facing mongoose version 6.0 So you just have to downgrade the mongoose version. Just run npm uninstall mongoose to uninstall the current mongoose version then run npm i [email protected], this will install the version that will fix your problem. Just check this link https://www.zhishibo.com/articles/132795.html

4

Mongoose has just updated and in 6+ version we have to pass the object as first parameter followed by call back function with params of error and result of query!!

To GET ALL records just pass the empty object.

    Album.find({}, function (err, result){ // get all albums
  if (err) { // if there will be any error
    console.log(err);
  } else { /// in success case in which records from DB is fetched
  console.log(result);
  }
});

https://mongoosejs.com/docs/api.html#model_Model.find

1
  • i also got the same! but just uninstalled the mongoose package and reinstall and it work like a charm (by using the latest documentation about the change in .find function)
    – sami ullah
    Commented Aug 30, 2021 at 20:36
0

Updating to [email protected] fixed the issue for me. npm run update --save will update all your dependencies including mongoose

Not the answer you're looking for? Browse other questions tagged or ask your own question.