0

I am new to mongoDB and mongoose, i have this simple document in 'users' collection in MongoLab:

{
    "_id": {
        "$oid": "55819f00e4b0a09388237163"
    },
    "name": "john"
}

In my express:

var mongoose = require('mongoose');
mongoose.connect('mongodb://ds047***.mongolab.com:47792/heroku_16lpd***');
mongoose.model('users', {name: String});
mongoose.model('users').find(function(err, users){
   console.log(users); => undefind
});

and getting undefined. could it be that my connection string is wrong?

shmnsw
  • 639
  • 2
  • 11
  • 24
  • Try replacing `mongoose.model('users'...` with `mongoose.model('user'...` - mongoose pluralizes model names. – x-ray Jun 17 '15 at 17:15
  • 2
    @x-ray Are you sure, as this sounds a bit insane? – Dejan Toteff Jun 17 '15 at 17:42
  • Just google "mongoose pluralization" and you will e.g. find this: http://stackoverflow.com/questions/7230953/what-are-mongoose-nodejs-pluralization-rules – x-ray Jun 17 '15 at 21:55

1 Answers1

0

I couldn't reproduce the scenario you are experiencing but I created a similar example which is working:

'use strict';

var mongoose = require('mongoose');
var User = mongoose.model('User', {name: String});

mongoose.connect('mongodb://localhost:27017/mydatabase', function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log('connected');
  }
});

User.create({name: 'Wilson'}, function () {

  User.find(function (err, users) {
    console.log(users);  // [{_id: 558240fbbfca399053238ac7, name: 'Wilson', __v: 0}]
  });

});
Wilson
  • 9,006
  • 3
  • 42
  • 46