Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to console.log in pug?

How can I console.log the data coming from the backend in pug?

For instance, this is my backend in expressjs:

    res.render("streams/show", {
        stream: cleanStream
    });

in show.pug, I want to inspect the data from steam:

- var species = stream.species;
- var fields = [];
- for (var key in species) fields.push(key)
- console.log(fields)

I can't see anything on my Developer Tool on my Chrome.

Any ideas?

like image 918
Run Avatar asked Oct 26 '16 12:10

Run


People also ask

How do I console log?

Steps to Open the Console Log in Google Chrome By default, the Inspect will open the "Elements" tab in the Developer Tools. Click on the "Console" tab which is to the right of "Elements". Now you can see the Console and any output that has been written to the Console log.

Can I console log function?

We can call any function inside a function, including console. log().

How do I get the console log in edge?

To log information to display in the Console: Open the demo webpage Console messages examples: log, info, error and warn in a new window or tab. To open the Console, press Ctrl + Shift + J (Windows, Linux) or Command + Option + J (macOS). Paste the above code into the Console, and then press Enter .


1 Answers

Your current method of accessing the data within the template will log information on the backend in the terminal where Express is running, not the frontend in Chrome Developer Tools.

In order to access the external information inside the template, you need to nest it inside a script tag and use JSON.stringify in combination with unescaped Pug string interpolation to render it in the HTML as below.

script
     | var species = !{JSON.stringify(stream.species)};
     | var fields = [];
     | for (var key in species) fields.push(key)
     | console.log(fields)
like image 51
litel Avatar answered Sep 17 '22 17:09

litel