【发布时间】:2016-02-06 05:20:38
【问题描述】:
创建数据库:
var mongojs = require('mongojs');
var db = mongojs('rodrigo-contatos', ['rodrigo-contatos']);
我正在尝试使用来自 mongojs 的 findOne 使用此代码在数据库中进行搜索,即代码:
app.get('/detalhesContato/:id', function(req, res){
var id = req.params.id;
console.log(id);
db.contatos.findOne({_id: mongojs.ObjectId(id)}, function(err, doc) {
console.log(err);
res.json(doc);
});
console.log(id) id 是正确的,但无论我做什么,findOne 都无法正常工作;(。
"567a16ba28dee028f4a8ad78
TypeError:无法读取未定义的属性“findOne” /Users/Michel/Documents/AngularProjects/RodrigoBranasListaTelefonica/server.js:48:12"
【问题讨论】:
-
好的,我做到了,你能看到吗?
-
问题是 contatos 未定义。试试 console.log(db),你会看到没有 contatos。我从来没有以你使用的方式使用过 mongo,但这里有一个猜测:db['rodrigo-contatos'].findOne
-
控制台日志(db)返回db没有问题
标签: javascript mongodb mongojs