How to output JSON with swig in nodejs

孤人 提交于 2020-01-02 03:49:45

问题


I tried json filter, but I get "json" filter not found error. What I'm I doing wrong?

Error: Filter "json" not found at Object.exports.wrapFilter (......./node_modules/swig/lib/helpers.js:310:11)
<script type="text/javascript">
        {{ places|json }}
</script>

object is passed from mongodb

this.displayMainPage = function(req, res, next) {
    "use strict";

    places.getPlaces(10, function(err, results) {
        "use strict";

        if (err) return next(err);

        return res.render('places_template', {
            places: results
        });
    });
}

Edit: I'm trying to output json to pass to google maps and display the same data in html


回答1:


For versions of Swig prior to 1.0, the json filter was instead named json_encode:

{{ places|json_encode }}

And, for compatibility, 1.0 and later keep json_encode as an alias of json.


Though, you should also consider upgrading Swig:

$ npm install swig@0.14
npm WARN deprecated swig@0.14.0: v1.0.0 is a complete rewrite of Swig
from the ground up. Previous versions are no longer supported


来源:https://stackoverflow.com/questions/20448700/how-to-output-json-with-swig-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!