Model.find () returns a null json object in mongoose (mongodb) on node

Here is the relevant code:

app.get('/all', function(req,res) {
  Party.find({},[],function(p) {
    console.log(p);
  });

  res.redirect('/');
});

should return all collections from the database - returns null in the console.

var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/impromptu');
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;

initialization overview

var PartySchema = new Schema({
what    : String,
when    : String,
where   : String
});

mongoose.model('Party',PartySchema);

// Models

var Party = db.model('Party');

scheme

I have everything that is necessary for the correct configuration, I can save collections just fine, I can’t get everything for some reason ...

Checked / var / log / mongodb.log and this is really a connection.

Any ideas?

+3
source share
1 answer

Assuming you use mongoose after v1.0 that null is an err argument for your callback (there are two ... first an error and then the results) ... Try this:

Party.find({},[],function(err,p) {
  console.log(p);
});
+7
source

Source: https://habr.com/ru/post/1795383/


All Articles