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?

3
  • retry with plain find with Roomlist.find(function(err, docs) {... Commented Jan 25, 2016 at 8: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. Commented Jan 25, 2016 at 8: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 Commented Jan 25, 2016 at 8:21

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.