如何在nodejs中使用swig输出JSON
我试过JSONfilter,但我得到“JSON”filter没有find错误。 我在做什么错了?
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
作为 json
的别名 。
虽然,你也应该考虑升级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