我尝试了 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 alias的 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
关于node.js - 如何在nodejs中用swig输出JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448700/