对于以下 GET请求,我试图让服务器返回所有"音乐家"文件,然后我将在音乐家 - 所有玉石模板页面上显示格式。
app.get("/musicians/all", function(req, res){
var id = req.params.id;
Musician.find(), function(err, data){
if (err){
res.render("error", {err: err});
} else {
console.log("here is the data: " + data);
res.render("musicians-all", {
musicians: data
});
}
};
});
我的模型设置为 Mongoose :
module.exports = function(mongoose){
var Schema = mongoose.Schema;
var MusicianSchema = new Schema({
firstName: String,
lastName: String,
country: String,
birthday: Date,
instruments: [],
});
mongoose.model("Musician", MusicianSchema);
}
我已经设置了一个html表单和发布请求,可以正确提交数据。
app.post("/musicians/create", function(req, res){
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var country = req.body.country;
var newMusician = new Musician;
newMusician.firstName = firstName;
newMusician.lastName = lastName;
newMusician.country = country;
newMusician.save(function(err, data){
if (err){
res.render("error", {err: err});
}
else {
var id = data.id;
res.redirect("/musicians/all");
console.log(firstName + lastName + "from" + country);
}
});
});
虽然我可以确认在提交html表单时将文档添加到MongoDb,但我不确定如何访问我要查找的文档。