[英]Render two MongoDB data to an EJS file
It's one of the data which I want to send to myaccount.ejs这是我要发送到 myaccount.ejs 的数据之一
app.get("/myaccount", (req, res)=>{
if(req.isAuthenticated()){
const user_files = File.find();
user_files.exec(function(err, data1){
if(err){
console.log(err);
} else {
res.render("myaccount", {file: data1});
}
})
This code below is the second data I want to send to myaccount.ejs.下面的代码是我要发送到 myaccount.ejs 的第二个数据。
app.get("/myaccount", (req, res)=>{
if(req.isAuthenticated()){
const user = User.find();
user.exec(function(err, data2){
if(err){
console.log(err);
} else {
res.render("myaccount", {user: data2});
}
})
How can I send both of them together to my ejs file.如何将它们一起发送到我的 ejs 文件。
You can use async/await
for cleaner code.您可以使用
async/await
来获得更简洁的代码。
app.get("/myaccount", async (req, res, next)=>{
try {
if(!req.isAuthenticated()){
throw new Error('Not Authenticated!');
}
const file = await File.find({}).exec();
const user = await User.find({}).exec();
if(!file || !user){
throw new Error('User or file not found.')
}
res.render("myaccount", {user, file});
}
catch(err){
return next(err);
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.