【发布时间】:2018-10-22 15:14:30
【问题描述】:
我正在做一个小型应用程序,从一些远程 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
如果我的英语听不懂,请原谅我说的是西班牙语。
【问题讨论】:
-
联赛在呈现个人资料页面时未定义。个人资料页面无需等待 get_league_data 从 api 获取数据即可呈现。
-
您的函数 get_league_data 和 get_sum_id 不返回任何内容,它们只是异步执行回调函数。这是一个非常常见的初学者错误——如果它是同步的,你根本无法获得异步数据。请参阅此相关问题:stackoverflow.com/questions/14220321/…
标签: javascript node.js express