0

I have a collection in mongoDB called 'roomlist'. Using the command line, i can run db.roomlist.find() and it will return the correct data. Example:

> db.roomlist.find()
{ "_id" : ObjectId("56a5d3b07bf609f092e7f9ab"), "name" : "Lobby", "type" : "public", "lock" : 0, "owner" : "Mike", "invites" : "all", "__v" : 0 }

However, in my node.js app, it just returns empty. If I console log the output, I get:

[]

Here's what I'm using:

My schema is as follows:

RoomSchema = new Schema({
    name: String,
    type: String,
    lock: String,
    owner: String,
    invites: String, created: {
        type: Date,
        default: Date.now
    }  
});

And then I define Roomlist as follows:

var Roomlist = db.model('Roomlist', RoomSchema);

I'm trying to retrieve a list of all "rooms" in the collection and send it as an object to one of my views. Here's what I've got for that:

Roomlist.find({},{}, function(err, docs) {
    if(err) throw err;    
    res.render('index.ejs',{               
        "roomlist": docs,
        csrfToken: req.csrfToken()
    });
    console.log(docs);
});

I have the console.log in there for testing, since I can't seem to get it to work. I have other schemas set up in the same file that are using the same approach, and they seem to work just fine. Could anyone tell me what I'm doing wrong here?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Mike Roberts
  • 161
  • 2
  • 14
  • retry with plain `find` with `Roomlist.find(function(err, docs) {...` – Vicky Gonsalves Jan 25 '16 at 08:14
  • 1
    Doesn't db.model("Roomlist") make a collection that looks like this : 'roomlists` .. it makes it into plural. but you have `db.roomlist.find()`.(singular) check your collections. – jack blank Jan 25 '16 at 08:16
  • @jackblank - I wasn't aware of the plural issue. I modified the collection and tested and it works now! Thank you so much! I knew it had to be something simple like that :) If you'll create that as an answer, I'll accept – Mike Roberts Jan 25 '16 at 08:21

1 Answers1

1

Doesn't db.model("Roomlist") make a collection that looks like this : 'roomlists` .. It makes it into plural. But you have db.roomlist.find().(singular) check your collections.

Here's a link that discusses the addition of the 's' at the end of a collection's name and a suggestion of how to make a collection without the "s"

Community
  • 1
  • 1
jack blank
  • 5,073
  • 7
  • 41
  • 73
  • Thank you again. All I had to do was add an 's' to my collection name and it works great. I really appreciate the tip! – Mike Roberts Jan 25 '16 at 08:25