[英]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.