javascript - 在 Express.JS 中呈现页面之前的异步请求
问题描述
我正在尝试在 express.js 中实现一个 HTTP 请求序列以用于培训目的。为了做到这一点,我使用 async/await 来等待我的请求并将它们包装在 try catch 块中。一切都按方面工作,因为我添加了一些 console.log 来检查流程。一切完成后,我想使用内置的 res.render() 函数渲染我的页面,但是在调用 res.render() [res.render 不是函数] 时会引发错误。
我可以通过 AJAX 加载数据,但这真的有必要吗?
/* Express Functions */
router
.get('/', async function (res,req,next){
try{
doSynchronousThings();
const ParticipantIds = await xingRequests.getParticipantIds();
console.log('...getParticipantIds response:');
console.log(ParticipantIds);
finalLog();
const ParticipantData = await xingRequests.getParticipantData(ParticipantIds[0]);
console.log('...getParticipantData response:');
console.log(ParticipantData);
console.log('...all done');
}catch(err){
console.error(err);
next(err);
}
res.render('test', {
page: {
title:'Xing Event API',
message: 'Happy Hacking!'
}
});
});
xingRequest 方法只是触发基于请求承诺的请求并返回响应对象。
解决方案
推荐阅读
- javascript - I'm trying to add animation to my web page title using anime.js and jQuery, but it throws an error
- jenkins - 无法从 Jenkins 中的参数设置环境变量
- excel - 在单独的工作表 Excel VBA 上将多个命名范围打印为 1 个 PDF
- kubernetes - Kuberenetes 部署的应用程序无法执行 swagger API
- java - Android:x天后获取日期
- r - 提取每个 id -R 的特定评论之前的所有行
- python - 有一种快速的方法可以知道 Anaconda 中是否安装了软件包
- python - 改变数据框的形状
- angular - 为什么没有输入对我的 http.get 的响应?
- java - 在 Java ThreadPoolExecutor 中在运行时更改线程池大小