Node.js通过mongoDB的ID查找结果
我在Node.js中是新的,所以对不起,如果这是真正愚蠢的问题,但是我尝试我找不到答案。
我有一些简单的代码:
var express = require("express"); var mongoose = require("mongoose"); var cors = require("cors"); mongoose.connect('mongodb://localhost/simple') var personSchema = { firstName:String, lastName:String, email:String } var Person = mongoose.model('Person', personSchema, 'people') var app = express(); app.use(cors()); app.get('/people', function (req, res) { Person.find(function (err, doc) { res.send(doc); }) }) app.get('/people/:id', function (req, res) { Person.findById(req.params.id, function (err, doc) { res.send(doc); }) }) app.listen(3000);
这应该返回一些我插入到mongo DB的数据,而第一个/ people /正确地返回一切(基本上只是返回数据库的转储)
带ID的第二个没有返回。 我试图debugging它,我看到里面的函数ID被正确定义,我也使用来自“人”网站(如: 55e028081181bfdd8a7972d2
)的ID,尽pipe这个事实,我无法得到任何答案或错误。
有没有机会可以告诉我哪里可以成为问题?
现在是时候把这个问题揭穿了,因为我对这里的错误回答感到厌倦。
代码作为一个单位是好的。 唯一可能发生的问题是你发送了错误的东西或者收集了错误的集合,以及关于_id
值本身的最后一个问题。
你需要的东西除了“原始”expression和mongoose这个工作,正如我的上市certificate:
var async = require('async'), express = require('express'), mongoose = require('mongoose'), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/simple'); var personSchema = new Schema({ firstName: String, lastName: String, email: String }); var Person = mongoose.model( 'Person', personSchema ); var app = express(); mongoose.connection.on('open',function(err) { async.series( [ function(callback) { Person.remove({},callback); }, function(callback) { Person.create({ firstName: "John", lastName: "Doe", email: "john.doe@example.com" },callback); } ], function(err,results) { if (err) throw err; console.log( "Created and call in URL: %s", results[1]._id ); // setup routes app.get('/people', function(req,res) { Person.find(function(err,docs) { res.send(docs) }); }); app.get('/people/:id', function(req,res) { Person.findById(req.params.id,function(err,doc) { res.send(doc); }); }); app.listen(3000); console.log("ready"); } ); });
其中给出如下输出:
Created and call in URL: 55e0464e66e13f6506e8e38e ready
所以,当你在/people
url上调用服务器时,你会得到:
[ { "_id":"55e0464e66e13f6506e8e38e", "firstName":"John", "lastName":"Doe", "email":"john.doe@example.com", "__v":0 } ]
作为回应。
当你在/people/55e0464e66e13f6506e8e38e
调用id时,你会得到:
{ "_id":"55e0464e66e13f6506e8e38e", "firstName":"John", "lastName":"Doe", "email":"john.doe@example.com", "__v":0 }
正如所料。
因此,如果您的应用程序无法正常工作,那么您要么不是以自己的想法发送,要么是实际上对集合中的_id
值具有string,或者在集合或数据库名称中完全不匹配。 所有的url都应该工作,如果没有人工作,那么后者就是这样。
在_id
是一个string的情况下,模式是错误的,并且需要:
var personSchema = new Schema({ _id: String firstName: String, lastName: String, email: String },{ "_id": false });
最重要的是,除了在模式中设置_id
字段的正确types外,还有{ "_id": false }
。
这是因为你需要这个来告诉mongoose不要试图把“string”parsing成一个ObjectId
因为它总是默认的。
没有这个设置,“string”将总是失败。
但是这个清单是什么工作的certificate,也是真正需要的唯一改变,如果实际上_id
包含“string”或其他types。 所以,如果一个Number
然后都设置_id: Number
的模式,也{ "_id": false }
否则相同的失败原因发生。
对于纯粹的愚蠢,然后改变我的列表:
var personSchema = new Schema({ _id: { type: String, default: "55e0464e66e13f6506e8e38e" }, firstName: String, lastName: String, email: String },{ "_id": false });
并且每次都看着它工作。
还请删除{ "_id": false }
并注意它失败,所以当_id
types被覆盖时,为什么这是必需的。
如果您已经从外部mongoose填充了数据库,那么您就有可能更改了_id
属性的types。 如果是这种情况,请尝试在您的schemma中添加_id,如下所示:
var personSchema = { firstName:String, lastName:String, email:String, _id:String }