Pass controller data to EJS

Asked

Viewed 268 times

1

I need to pass the obtained data, by a query in the database, to my EJS template in order to manipulate these values.

My current code is like this

router.get('/devreport', redirectLogin, (req, res) => {
    //todo trocar o valor para o codigo do sg ao invés de 1. mockado para finalizar
    let cod_sg = 1;
    let heuristic_values = [];
    let selectQuery = `SELECT * 
                        FROM formulario
                        WHERE cod_sg = ?`

    let queryValues = [cod_sg];

    execSQLQuery(selectQuery, queryValues)
        .then(dbResponse => {
                heuristic_values.push({
                    values: dbResponse[0].heuristic_responses
                });            
            console.log(heuristic_values);
            res.render('../views/devreport.ejs', { heuristic_values });
        })
        .catch(error => {
            console.log(error);
            console.log(req.session.userId);
            res.json('fuck');
        });

    /* req.session.cod_sg = null; */

    res.render('../views/devreport.ejs');
});

Console result.log(heuristic_values);

[ { values: '{"likert-one": "strong_agree", "likert-six": "strong_agree", "likert-ten": "strong_disagree", "likert-two": "agree", "likert-five": "agree", "likert-four": "neutral", "likert-nine": "strong_agree", "likert-eight": "neutral", "likert-seven": "neutral", "likert-three": "strong_disagree", "likert-eleven": "agree", "likert-twelve": "strong_disagree", "likert-fifteen": "neutral", "likert-sixteen": "neutral", "likert-fourteen": "neutral", "likert-thirteen": "neutral", "likert-seventeen": "neutral"}' } ]

However, when I arrive at the page to which I rendered these values, I get the following return

ReferenceError: C:\Users\konex\Videos\ExpertSystem\views\devreport.ejs:14
    12| 

    13|     <ul>

 >> 14|             <%  const responses = heuristic_values %>

    15| 

    16|     </ul>

    17| 


heuristic_values is not defined
    at eval (eval at compile (C:\Users\konex\Videos\ExpertSystem\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:11:27)
    at returnedFn (C:\Users\konex\Videos\ExpertSystem\node_modules\ejs\lib\ejs.js:653:17)
    at tryHandleCache (C:\Users\konex\Videos\ExpertSystem\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\konex\Videos\ExpertSystem\node_modules\ejs\lib\ejs.js:482:10)
    at View.render (C:\Users\konex\Videos\ExpertSystem\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\konex\Videos\ExpertSystem\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\konex\Videos\ExpertSystem\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\konex\Videos\ExpertSystem\node_modules\express\lib\response.js:1008:7)
    at router.get (C:\Users\konex\Videos\ExpertSystem\controllers\home_controller.js:248:9)
    at Layer.handle [as handle_request] (C:\Users\konex\Videos\ExpertSystem\node_modules\express\lib\router\layer.js:95:5)

2 answers

1

You’re just passing the content heuristic_values in the rendering of ejs and not the variable itself, if you want to access a variable in your view you must specify its name as follows:

res.render('../views/devreport.ejs', { 'heuristic_values' : heuristic_values });

Here is passed a variable 'heuristic_values' nominee.

When you just passed the heuristic_values you only passed the content of it, to take the proof and understand the form of data passing in your view(devreports.ejs) in your code as is instead of putting your variable put one of the values contained, as likert-one, you will see the content printed on the screen.

  • Dude, I made the changes you requested and I’m still having the same problem. When placing <% const Sponses = Likert-one %>, the error updates to Likert is not defined

  • 1

    Guy was just an example, but do the following on the line you render the ejs,res.render('../views/devreport.ejs', { heuristic_values }); you change to &#xA;res.render('../views/devreport.ejs', { 'heuristic_values' : heuristic_values });, in your view you put <% const responses = heuristic_values %> now should work

  • Gee, man, I’d done just that :( Thanks so much for trying to help me out

0

identified the problem. I tested and it is OK!

The reason was, I was using the res.render twice. The first time, I rendered with the variable heuristic_values. The second time, I would render without. Because of this, the reference was lost.

res.render('../views/devreport.ejs', { heuristic_values });

res.render('../views/devreport.ejs');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.