简体   繁体   English

如何从多个节点请求主体中获取数据并传递到 EJS 文件中

[英]How do I take data from multiple node-request bodies and pass into an EJS file

I am creating a website which uses an api to dispaly football (soccer) scores, fixtures etc. I have tried to create a global variable to save the request data to and then pass into ejs however this doesn't seem to work.我正在创建一个网站,它使用 api 来显示足球(足球)比分、赛程等。我试图创建一个全局变量来将请求数据保存到然后传递给 ejs,但这似乎不起作用。

router.get('/stats', (req, res) => {


request(options, function (error, response, body) {
    if (error) throw new Error(error);

  top_scorer_data = JSON.parse(body)

});


request(options2, function (error, response, body) {
    if (error) throw new Error(error);

  top_assists_data = JSON.parse(body)

});


request(options3, function (error, response, body) {
    if (error) throw new Error(error);

  top_red_cards_data = JSON.parse(body)  

});


request(options4, function (error, response, body) {
    if (error) throw new Error(error);

  top_yellow_cards_data = JSON.parse(body)

});

res.render('bundesliga/stats', {})

})

You should follow DRY principle and move request to a service or a helper function.您应该遵循 DRY 原则并将request移动到服务或助手 function。 You shouldn't use global variables, use promises instead, and wait for all requests to finish.您不应该使用全局变量,而是使用 Promise,并等待所有请求完成。

So, you should promisify request method (you should really migrate to axios or node-fetch , because request is deprecated)所以,你应该承诺 request 方法(你应该真正迁移到axiosnode-fetch ,因为request已被弃用)

and then use Promise.all to make all the requests with each option, and then gather results into variables, which you then pass to the .render method.然后使用Promise.all使用每个选项发出所有请求,然后将结果收集到变量中,然后将其传递给.render方法。

Try this:尝试这个:

router.get('/stats', async(req, res) => {

    function getData(options) {
        return new Promise((resolve, reject) => {
            request(options, function(error, response, body) {
                if (error) reject(error);
                resolve(JSON.parse(body));
            });
        });
    }



    const [top_scorer_data, top_assists_data, top_red_cards_data, top_yellow_cards_data] = await Promise.all([
        getData(options),
        getData(options2),
        getData(options3),
        getData(options4)
    ]).catch(err => {
        console.log('err', err);
    });

    res.render('bundesliga/stats', {
        top_scorer_data,
        top_assists_data,
        top_red_cards_data,
        top_yellow_cards_data
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node-request ip addres与远程ip地址不同 - node-request ip addres different from remote ip address 如何将数据传递到Sails和node.js中的ejs文件? - How to pass data to ejs file in sails & node.js? 在node.js中重定向时如何将数据从express传递到.ejs文件 - How to pass Data from express to .ejs file while redirecting in node.js 我如何将 .js 文件传递到 .ejs 和节点 - How can i pass a .js file into .ejs and node 如何在节点js中将纬度和经度从数据库传递到ejs文件 - How to pass latitude and logitude from database to ejs file in node js 如何将数据从ejs(日期选择器)传递到js(以查询数据库),然后将结果显示回ejs(在标签中)? - How do i pass data from ejs(date picker) to js(to query db) and then display results back in ejs(in a label)? 如何使用回调从带有 node.js 和 ejs 的 mysql 获取数据? - How do I use a callback to fetch data from mysql with node.js and ejs? NodeJs 使用 PHP 和 EJS 作为模板引擎,我如何从路由到模板和在模板中传递和访问数据? - NodeJs using PHP and EJS as template engine, how do i pass and access data from routes to and in template? 如何使用 EJS 将对象从 NodeJS 传递到 javascript 文件 - How do I pass objects from NodeJS to javascript files with EJS 如何将数据从一个 app.js 文件发送到 Routes 中的 index.ejs 文件 - how do I send data from one app.js file to index.ejs file in Routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM