问题描述
我正在开发一个网站,该网站在主页上显示不同类别组中的几部电影,因此我尝试使用多个查找功能(猫鼬)并将它们传递到渲染中以将它们一起显示在视图中。问题是,如果我尝试将 find 函数中的电影数组存储在变量或 res.locals 中,它只是保持“未定义”。我似乎无法弄清楚如何在一个 get 响应中传递这些数组中的几个。我是 Node 的新手,感谢您的帮助!
app.get("/",(req,res) => {
// array of top-selling movies
Movie.find({
group: "Top-Selling Movies"
},function(err,movies) {
if (err) {
console.log(err);
} else {
// In EJS,use found items array and tap into attributes to display in frontend
res.locals.topMovies = movies;
}
});
// array of new movies
Movie.find({
group: "New rental movies"
},use found items array and tap into attributes to display in frontend
res.locals.newMovies = movies;
}
});
// array of recommended movies
Movie.find({
group: "Recommended For You"
},use found items array and tap into attributes to display in frontend
res.locals.recMovies = movies;
}
});
// array of action/thrilling movies
Movie.find({
group: "Thrilling movies"
},use found items array and tap into attributes to display in frontend
res.locals.actionMovies = movies;
}
});
res.render("categories");
});
我什至尝试过:
app.get("/",res) => {
var topMovies;
var newMovies;
var recMovies;
var actionMovies;
// array of top-selling movies
Movie.find({
group: "Top-Selling Movies"
},use found items array and tap into attributes to display in frontend
topMovies = movies;
}
});
// array of new movies
Movie.find({
group: "New rental movies"
},use found items array and tap into attributes to display in frontend
newMovies = movies;
}
});
// array of recommended movies
Movie.find({
group: "Recommended For You"
},use found items array and tap into attributes to display in frontend
recMovies = movies;
}
});
// array of action/thrilling movies
Movie.find({
group: "Thrilling movies"
},use found items array and tap into attributes to display in frontend
actionMovies = movies;
}
});
res.render("categories",{
topMovies: topMovies,newMovies: newMovies,recMovies: recMovies,actionMovies: actionMovies
});
解决方法
您可以为此使用 async/await
app.get("/",async (req,res) => {
const topMovies = await Movie.find({ group: "Top-Selling Movies" })
const newMovies = await Movie.find({ group: "New rental movies" })
const recMovies = await Movie.find({ group: "Recommended For You" })
const actionMovies = await Movie.find({ group: "Thrilling movies" })
return res.render("categories",{
topMovies: topMovies,newMovies: newMovies,recMovies: recMovies,actionMovies: actionMovies
});
});
或者为了更好看的代码
app.get("/",res) => {
const [topMovies,newMovies,recMovies,actionMovies] = await Promise.all([
Movie.find({ group: "Top-Selling Movies" }),Movie.find({ group: "New rental movies" }),Movie.find({ group: "Recommended For You" }),Movie.find({ group: "Thrilling movies" })
])
return res.render("categories",{
topMovies,actionMovies
});
});
编辑:同样在您的示例中,您可以仅使用过滤器方法来限制数据库查询
app.get("/",res) => {
const movies = await Movie.find()
const topMovies = movies.filter(movie => movie.group === 'Top-Selling Movies');
const newMovies = movies.filter(movie => movie.group === 'New rental movies');
const recMovies = movies.filter(movie => movie.group === 'Recommended For You');
const actionMovies = movies.filter(movie => movie.group === 'Thrilling movies');
return res.render("categories",actionMovies
});
});