Mongoose find() not returning result

前端 未结 1 1823
礼貌的吻别
礼貌的吻别 2020-11-28 11:49

I have a route set up that uses a model called Todo like below:

app.get(\'/api/todos\', function(req, res) {
    Todo.find({},function(err, todos) {
                 


        
相关标签:
1条回答
  • 2020-11-28 12:47

    So what this very much looks like is that you have already created collections in an existing database and now you are trying to access these with mongoose models.

    The problem is that mongoose uses some defaults which you may not be aware of, so the example you are showing from the shell is not the same as what mongoose is doing by default.

    So you can either rename your collections to match what mongoose expects by default or change what mongoose does to match your existing names. In the latter case, you directly define the model names like so:

    mongoose.model( "Todo", toDoSchema, "Todo" );
    

    So the third argument to the method actually specifies the explicit name to use for the collection. Without this the assumed name under the default rules will be "todos".

    Use either method in order yo make them match.

    0 讨论(0)
提交回复
热议问题