find({}) returns 一个空数组猫鼬
find({}) returns an empty array mongoose
这是我在 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))
应该 return 一个包含 docs
中所有对象的数组,但它 return 是一个空数组。
您缺少架构创建部分,使用 mongoose.Schema
、
const schoolNotices = mongoose.model("schoolNotices",
new mongoose.Schema(
{
title:{
type: String
},
date:{
type:String
},
details:{
type:String
}
},
{ collection: "schoolNotices" } // optional
)
);
这是我在 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))
应该 return 一个包含 docs
中所有对象的数组,但它 return 是一个空数组。
您缺少架构创建部分,使用 mongoose.Schema
、
const schoolNotices = mongoose.model("schoolNotices",
new mongoose.Schema(
{
title:{
type: String
},
date:{
type:String
},
details:{
type:String
}
},
{ collection: "schoolNotices" } // optional
)
);