node.js - 如何在nodejs中用swig输出JSON

标签 node.js swig-template

我尝试了 json 过滤器,但出现“找不到 json”过滤器的错误。我做错了什么?

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

对象从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
        });
    });
}

编辑:我正在尝试输出 json 以传递给谷歌地图并在 html 中显示相同的数据

最佳答案

对于 1.0 之前的 Swig 版本,json filter而是命名为 json_encode :

{{ places|json_encode }}

并且,为了兼容性,1.0 及更高版本保留 json_encode as an aliasjson


不过,您还应该考虑升级 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

关于node.js - 如何在nodejs中用swig输出JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448700/

相关文章:

angularjs - 是否可以使用带有表单搜索查询输入文本的 Angular Controller 通过 Mongoose 在 MongoDB 中进行实时搜索?

node.js - Zappa 错误无法找到 View ( Jade 模板)

node.js - 如何使查询结果可从所有页面访问

node.js - Nodejs/Swig If then 扩展(条件继承)

javascript - 需要在for循环中隐藏html输入

javascript - 如何使用 Socket.io 在一条消息中发送两个变量?

node.js - 需要帮助了解 gulp 和 webpack-stream 的工作原理

javascript - Node + Express + Swig/将本​​地 json 文件传递​​给模板

javascript - Nodejs swig、子字符串或限制字符

node.js - 如何从基于 promise 的 Passport 本地策略中获得值(value)?