我是mongoDB和mongoose的新手,我在MongoLab的'users'集合中有这个简单的文档:
{
"_id": {
"$oid": "55819f00e4b0a09388237163"
},
"name": "john"
}
在我的表达中:
var mongoose = require('mongoose');
mongoose.connect('mongodb://ds047***.mongolab.com:47792/heroku_16lpd***');
mongoose.model('users', {name: String});
mongoose.model('users').find(function(err, users){
console.log(users); => undefind
});
并且未定义。可能是我的连接字符串错了吗?
答案 0 :(得分:0)
我无法重现您遇到的情景,但我创建了一个类似的示例:
'use strict';
var mongoose = require('mongoose');
var User = mongoose.model('User', {name: String});
mongoose.connect('mongodb://localhost:27017/mydatabase', function (err) {
if (err) {
console.log(err);
} else {
console.log('connected');
}
});
User.create({name: 'Wilson'}, function () {
User.find(function (err, users) {
console.log(users); // [{_id: 558240fbbfca399053238ac7, name: 'Wilson', __v: 0}]
});
});