我正在做一个小型应用程序,它从一些远程JSON文件中获取一些数据,其想法是生成一些统计信息,这些统计信息随后会打印在EJS文件中。
我想分别传递值进行渲染,然后在文档ejs中使用它们。 服务器文件运行良好,除了我有一个模块(我在其中开发功能)和一个脚本(我在其中有路由并执行它们)之外,
//rapi.js
const extjson = require ('remote-json');
//---------------------API CONFIG--------------------------
//apikey
const apikey ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
function get_sum_id(sumname, callback){
const urlsumbySumName = "https://la2.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + sumname + "?api_key=" + apikey;
extjson(urlsumbySumName).get(callback);
}
function get_league_data(sumid, callback){
const urlgetleaguedata ="https://la2.api.riotgames.com/lol/league/v3/positions/by-summoner/"+ sumid + "?api_key="+ apikey;
extjson(urlgetleaguedata).get(callback)
}
module.exports = { get_sum_id, get_league_data};
//---------------------------------------------------------------------
//index.js
const riot = require('./rapi.js');
const express = require('express');
const router = express.Router();
router.get('/',async (req, res) => {
res.render('index');
});
router.post('/profile',async (req, res, next)=>{
const sum = req.body.summoners; //from html form
const sum_id = riot.get_sum_id(sum, function(err, resp, body){body.id});
res.render('profile', {sum,
id: sum_id,
league: riot.get_league_data(sum_id, function(err,resp,body){body})
});
});
module.exports = router;
我想做的就是将回调函数的值直接传递给渲染器并引用它。显然我在做错事,因为它不起作用。
输出:
> in localhost:3000/profile - internal server error. in console:
> TypeError:
> C:\xampp\htdocs\proyectos\legendsop\src\views\profile.ejs:80
> 78| <section class="row border">
> 79| <section class="col-2 border">Perfil</section>
> >> 80| <section class="col-1 border">SOLO/DUO Q <br><%= league[1].tier%></section>
> 81| <section class="col-1 border">FLEX <br><%= league[0].tier%></section>
> 82| <section class="col-4 border">WinRateRolCola</section>
> 83| <section class="col-2 border">EstadisticasCampeones</section>
>
> Cannot read property '1' of undefined
>
> at eval (eval at compile (C:\xampp\htdocs\proyectos\legendsop\node_modules\ejs\lib\ejs.js:618:12),
> <anonymous>:16:32)
> at returnedFn (C:\xampp\htdocs\proyectos\legendsop\node_modules\ejs\lib\ejs.js:653:17)
> at tryHandleCache (C:\xampp\htdocs\proyectos\legendsop\node_modules\ejs\lib\ejs.js:251:36)
> at View.exports.renderFile [as engine] (C:\xampp\htdocs\proyectos\legendsop\node_modules\ejs\lib\ejs.js:482:10)
> at View.render (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\view.js:135:8)
> at tryRender (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\application.js:640:10)
> at Function.render (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\application.js:592:3)
> at ServerResponse.render (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\response.js:1008:7)
> at router.post (C:\xampp\htdocs\proyectos\legendsop\src\routes\index.js:14:5)
> at Layer.handle [as handle_request] (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\layer.js:95:5)
> at next (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\route.js:137:13)
> at Route.dispatch (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\route.js:112:3)
> at Layer.handle [as handle_request] (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\layer.js:95:5)
> at C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\index.js:281:22
> at Function.process_params (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\index.js:335:12)
> at next (C:\xampp\htdocs\proyectos\legendsop\node_modules\express\lib\router\index.js:275:10)
>
> Git GitHub Initialize a new project directory with a Git repository
> Create repository
对不起,如果我的英语不好,我会说西班牙语。
答案 0 :(得分:0)
这可能对您有帮助 The purpose of the res.render callback argument
再次在回调中呈现ejs文件 希望这可以帮助 欢呼