如何使用node / mongoose正确检索mongodb数据

时间:2017-07-21 00:22:53

标签: node.js mongodb mongoose

在我的节点应用程序中,我有如下的server.js:

// modules =================================================
var express        = require('express');
var app            = express();
var mongoose       = require('mongoose');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');

// configuration ===========================================
var mongoDB = 'mongodb://root:root@localhost:27017/dms';
mongoose.connect(mongoDB);

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'MongoDB connection error:'));


db.once('open', function () {
    var tasksSchema = new mongoose.Schema({
        What: String,
        Who: String,
        When: Date
    });
    var Tasks = db.model('Tasks', tasksSchema);
    Tasks.find({}, function (err, tasks) {
        if (err) {
            onErr(err, callback);
        } else {
            mongoose.connection.close();
            console.log(tasks);
            //callback("", tasks);
        }
    });
});

var port = process.env.PORT || 9080; // set our port
// mongoose.connect(db.url); // connect to our mongoDB database (commented out after you enter in your own credentials)

// get all data/stuff of the body (POST) parameters
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users

// routes ==================================================
require('./app/routes')(app); // pass our application into our routes

// start app ===============================================
app.listen(port);
console.log('Magic happens on port ' + port);           // shoutout to the user
exports = module.exports = app;                         // expose app

当这个应用程序启动时,这行代码:console.log(tasks); 产生一个空数组:[]:

/usr/local/bin/node --debug-brk=49753 --nolazy server.js
Debugger listening on port 49753
`open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
Magic happens on port 9080
Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
[]

从mongodb命令行运行时,同一查询会生成一个文档:

Eugenes-MacBook-Pro-2:~ eugene$ mongo dms -u "root" -p "root"
MongoDB shell version: 3.0.4
connecting to: dms
> db.Tasks.find()
{ "_id" : ObjectId("59712d3f65283137a1676345"), "What" : "what-1", "Who" : "who-1", "When" : ISODate("2014-01-16T00:00:00Z") }
> 

我可以在节点/猫鼬代码中更改什么,让它输出相同的文档?

0 个答案:

没有答案