在Express.JS中呈现Page之前的异步请求

时间:2019-01-24 10:37:23

标签: javascript express async-await

我正试图在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方法仅触发基于请求承诺的请求并返回响应对象。

0 个答案:

没有答案
相关问题