我在這里通過mongodb的ID來查詢數(shù)據(jù)我的步驟:1、引包var ObjectId = require('mongodb').ObjectId;2、ObjectId(_id)3、查詢但是我ObjectId(_id)包裝后的ID的值就改變了,不是包裝的前的ID了,查詢的結(jié)果也為空var?ObjectId?=?require('mongodb').ObjectId;
exports.getArticleById?=?function?(req,res,next)?{
????var?id=?req.query.id;
????var?_id?=??ObjectId(_id);
????
????console.log(id);
????console.log(typeof(id));
????console.log("包裝后的ID:"+_id);
????console.log(typeof(_id));
????
????db.find("article",?{"id":?_id},?function?(err,?result)?{
????????if?(err)?{
????????????res.send("文章被刪除");?
????????????return
????????}
????????console.log(result);
????});
????res.render('article/article',?{
????????"login":?req.session.login?==?"1"???true?:?false,
????????"username":?req.session.login?==?"1"???req.session.username?:?""
????});
};這是結(jié)果:有毒,這里哪里用錯了嗎?然后我換mongoose來轉(zhuǎn)就成功了,數(shù)據(jù)也拿到了var?mongoose?=?require('mongoose');
var?_id?=?mongoose.Types.ObjectId(id);
- 1 回答
- 0 關(guān)注
- 2284 瀏覽
添加回答
舉報
0/150
提交
取消