如何在Node.js中使用Swig输出JSON [英] How to output JSON with swig in nodejs

查看:118
本文介绍了如何在Node.js中使用Swig输出JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了json过滤器,但出现找不到json"过滤器的错误.我在做什么错了?

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>

对象是从mongodb传递的

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
        });
    });
}

我正在尝试输出json以传递到Google地图并在html中显示相同的数据

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

推荐答案

对于1.0之前的Swig版本, json过滤器改名为 json_encode :

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

{{ places|json_encode }}

出于兼容性考虑,1.0和更高版本将json_encode 保留为别名(json.

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

尽管如此,您还应该考虑升级Swig:

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

这篇关于如何在Node.js中使用Swig输出JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆