18

I just started learning MongoDB and mongoose. Currently I have the following structure:

database   -> skeletonDatabase
collection -> adminLogin

When I run db.adminLogin.find() from the command line I get:

{ "_id" : ObjectId("52lhafkjasfadsfea"), "username" : "xxxx", "password" : "xxxx" }

My connection (this works, just adding it FYI)

module.exports = function(mongoose)
{
    mongoose.connect('mongodb://localhost/skeletonDatabase');

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
        console.log('Conntected To Mongo Database');
    });
}

My -js-

module.exports = function(mongoose)
{
    var Schema = mongoose.Schema;

    // login schema
    var adminLogin = new Schema({
        username: String,
        password: String
    });

    var adminLoginModel = mongoose.model('adminLogin', adminLogin);
    var adminLogin = mongoose.model("adminLogin");

    adminLogin.find({}, function(err, data){
        console.log(">>>> " + data );
    });
}

My console.log() returns as >>>>

So what am I doing wrong here? Why do I not get any data in my console log? Thanks in advance for any help.

khollenbeck
  • 16,028
  • 18
  • 66
  • 101

3 Answers3

32

mongoose by default takes singular model names and pairs them with a collection named with the plural of that, so mongoose is looking in the db for a collection called "adminLogins" which doesn't exist. You can specify your collection name as the 2nd argument when defining your schema:

var adminLogin = new Schema({
    username: String,
    password: String
}, {collection: 'adminLogin'});
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 3
    It's a terrible anti-feature. I think I've seen talk on github of putting a stop to it in some future release. If you are using express, `res.send(data)` to send it to the browser and you're done. – Peter Lyons Oct 28 '13 at 05:43
  • 1
    Nice to know, yeah I am using express. But I am a little foggy on that as well. I tried using `res.send(data)` but couldn't quiet figure out how the whole response thing was suppose to work. I have my app.configure global along with my mongoose connection. So I am using `require()` but wasn't sure how to pass a request through it. – khollenbeck Oct 28 '13 at 05:50
2

Had a problem with injecting it within an express route for my api so I changed it thanks to @elkhrz by first defining the schema and then compiling that one model I want to then pull like so:

app.get('/lists/stored-api', (req, res) => {

    Apis.find(function(err, apis) {

        if (err) return console.error(err);

        res.send(apis);

    });

});

I wouldn't send it to the body, I would actually do something else with it especially if you plan on making your API a production based application.

Run through this problem and read up on possible proper ways of rendering your data: How to Pass Data Between Routes in Express

Always a good idea to practice safe procedures when handling data.

1

first compile just one model with the schema as an argument

var adminLogin = mongoose.model('adminLogin', adminLogin);

in your code adminLogin does not exist, adminLoginModel does;

after that ,instead to

adminLogin.find({}, function(err, data){
        console.log(">>>> " + data );
    });

try this

adminLogin.find(function (err, adminLogins) {
  if (err) return console.error(err);
  console.log(adminLogins);

is important the "s" because mongo use the plural of the model to name the collection, sorry for my english...

elkhrz
  • 11
  • 2