最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to console.log in pug? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Oct 26, 2016 at 12:15 RunRun 57.2k177 gold badges463 silver badges771 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

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)
发布评论

评论列表(0)

  1. 暂无评论