find documents through Model in mongoose

Viewed 66

I'm learning mongoose and decided to run this code

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test');

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

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

const person = new Person({
    name: 'John',
    age: 25,
});

person.save().then(()=> console.log('inserted'));

Person.find( function (err, docs) {
    if(err){
        console.log(err);
    }else{
        console.log(docs);
    }
});

However it seems is not working as I expected (I want to print every document of the collection related to the model Person). Instead I am getting this warning/error:

(node:645135) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments at Collection.find ...

1 Answers

Two arguments. First one is condition, second one is function.

Person.find({ }, function (err, docs) { 
    if(err){
        console.log(err);
    }else{
        console.log(docs);
    }
}); 
Related