简体   繁体   中英

Mongoose and MongoDB - Can't seem to find documents in my collection

Title says it all. I'm trying to learn MongoDB and use the find() function to select all documents in my Comments collection.

I'm trying to use Mongoose directly in my routes.js file, if there's a cleaner convention please let me know, I'm new to NodeJS in general...


routes.js

var express = require("express"),
    router = express.Router(),
    mongoose = require("mongoose"),
    Comment = require("../models/Comment");

router.get("/comments", function(req, res, next)
{
    mongoose.set("debug", true);
    mongoose.connect("mongodb://127.0.0.1/Comments");

    var comments = [];

    Comment.find(function(err, array)
    {
        var i = 0;

        for(var o in array)
        {
            comments[i] = o;
            i++;
            console.log("Found comment!");
        }
    });

    res.render("comments", {
        comments: comments
    });

    mongoose.connection.close();
});

module.exports = router;

Comment.js (model)

var mongoose = require("mongoose");

var CommentSchema = new mongoose.Schema({
    author: String,
    message: String,
    posted: Date
});

// Name of the model, the schema, and the collection
module.exports = mongoose.model("Comment", CommentSchema, "Comments");

As you can see in my routes.js file, I have debug set to true, so whenever I request the /comments resource, I get the following output:

Comments.find()返回未定义

I can't make sense of this output, other than the fact that the query is reaching the Mongo daemon, so I'm guessing I have either imported my model incorrectly, or I'm just using Mongoose incorrectly. My Comments collection is populated with data; I can connect through the Mongo CLI and use db.Comments.find() and the documents are shown.

I have omitted my view as not to bloat the question. Thank you all for your time.


Solution:

routes.js (fixed)

var express = require("express"),
    router = express.Router(),
    mongoose = require("mongoose"),
    Comment = require("../models/Comment");

mongoose.set("debug", true);
mongoose.connect("mongodb://127.0.0.1/Comments");  

router.get("/comments", function(req, res, next)
{    
    Comment.find(function(err, dataset)
    {
        res.render("comments", {
            comments: dataset
        });
    });
});

module.exports = router;

It should be noted that mongoose.model.find() is asynchronous, so I had to move my Express render operation within the find() callback, otherwise my dataset variable would be rendered before it was populated.

Comment.js was errorless and thus was not updated for this solution.

The problem is that you're calling mongoose.connection.close() before your async Comment.find call completes.

Remove the mongoose.connection.close() call and move your mongoose.connect call to your app's startup code. Just leave the created connection pool open for the life of your app rather than opening and closing it with each request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM