【发布时间】:2021-01-12 20:18:08
【问题描述】:
这是我在 model.js 文件中的代码:
const mongoose = require("mongoose");
const con = require("./connection");
con();
const schoolNotices = mongoose.model("schoolNotices",
{
title:{
type: String
},
date:{
type:String
},
details:{
type:String
}
});
module.exports = schoolNotices;
我已经将此代码导入到students.js文件中
router.route("/notices")
.get((req,res)=>{
schoolNotices.find({},(err,docs)=>{
if(err){
return console.log(err)
}
res.render("studentNotices",{title:"Notices",data:docs})
})
}).post(urlencodedParser,(req,res)=>{
});
schoolNotices 集合有 3 个对象,例如:
{
"_id" : ObjectId("5ffa80077245b1208057a4ca"),
"title" : "annual sports day",
"date" : "03-01-2021",
"details" : "The point of using Lorem Ipsum is that it has a more-or-less normal distribution of
letters, as opposed to using 'Content here, content here', making it look like readable English.
Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model
text"
}
find({},(err,docs)) 应该返回一个包含docs 中所有对象的数组,但它返回一个空数组。
【问题讨论】:
标签: node.js mongodb mongoose find