[英]Node && Mongodb findOne is not working
无法读取null的属性“标题”! 正在显示,而正在重新查找findone.ejs文件。。但是,db中的第一个显示得很完美。
app.get('/books/:title', (req, res) => {
db.collection("Book")
.findOne({ 'title': req.params.title }, function(err, result) {
if (err) throw err;
res.render('findone.ejs', { Book: result});
});
})
数据库模式:var mongoose = require('mongoose'); var Schema = mongoose.Schema;
var BookSchema = new Schema({
title: String,
author: String,
category: String
});
module.exports = mongoose.model('Book', BookSchema);
Mongo数据库。
{
"_id": {
"$oid": "5a14a5edf6fe123247b890f3"
},
"title": "ttt",
"author": "ttttt",
"category": "tttttttttttt"
}
{
"_id": {
"$oid": "5a14a5f2f6fe123247b890f4"
},
"title": "tttt",
"author": "ttttt",
"category": "ttttttttttttttttttttttttttt"
}
{
"_id": {
"$oid": "5a154e4bff45fe2c9035f9da"
},
"title": "hello ",
"author": "rabbani",
"category": "how are you"
}
如果您使用猫鼬,则可以像这样导入架构
Book = mongoose.model('Book')
并像这样查询
Book.findOne({"title": title}, function(err, book) {
//handle book
})
当然,您必须确保标题是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.