javascript - Render two MongoDB data to an EJS file
问题描述
It's one of the data which I want to send to 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.
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.
解决方案
You can use async/await
for cleaner code.
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);
}
})
推荐阅读
- abaqus - 如何使用 Abaqus python 获取表面节点?
- r - 如何将 geom_label 放入 geom_bar
- python - Intersphinx Docstring 可读性
- asp.net-mvc - Asp.net core 表单总是返回 null
- google-apps-script - 在 Google Apps 脚本中创建 Geochart 时“请求的地图不存在”
- c++ - 如何确保可变元组中的类型相同?
- javascript - 如何在工具栏中隐藏/不显示 PDF 标题
- ios - UIAlertController 内的键盘未关闭
- python - 尝试使用 Python 打开 Photoshop 时出现 com_error -2146959355
- git - 新的工作树和分支有什么区别?