0

I'm doing a small application bringing some data from some remote JSON files and the idea is to generate some statistics that are later printed in an EJS file.

I want to pass separately values ​​to render and then use them in the document ejs. The server file works well, apart from that I have a module in which I develop the functions and a script where I have the routes and execute them:

 //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;

what I want to do as seen there is to pass the value of that callback directly to the render and reference it. Obviously something I'm doing wrong since it does not work.

outputs:

> 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

Excuse me if my English is not understood very well I am Spanish speaking.

Lord Wolfur
  • 93
  • 3
  • 12
  • League is undefined at the time of rendering the profile page. The profile page gets rendered without waiting for get_league_data to get data from api. – Rohit Nethi Oct 22 '18 at 15:22
  • Your functions *get_league_data* and *get_sum_id* do not return anything, they just execute callback functions asynchronously. This is a very common beginner's mistake – you simply cannot get asynchronous data is if it were synchronous. See this related question: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Patrick Hund Oct 22 '18 at 15:22
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Hund Oct 22 '18 at 15:24

1 Answers1

0

This might help you The purpose of the res.render callback argument

Render the ejs file in callback again hope this helps cheers