Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here's 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;

general stuff about initialization

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

mongoose.model('Party',PartySchema);

// Models

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

schema

I have everything else for it setup properly, I can save collections just fine, can't retrieve all for some reason...

Checked /var/log/mongodb.log and it is indeed connecting.

Any ideas?

like image 887
acrognale Avatar asked Jun 20 '26 12:06

acrognale


1 Answers

Assuming you're using mongoose after v1.0 that null is the err argument to your callback (there are two ... first the error then the results) ... Try this:

Party.find({},[],function(err,p) {
  console.log(p);
});
like image 76
Travis Avatar answered Jun 23 '26 00:06

Travis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!