Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output JSON with swig in nodejs

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

like image 246
Vlad Vinnikov Avatar asked Dec 08 '13 01:12

Vlad Vinnikov


People also ask

How do I display json data in node JS?

To load the data from customer. json file, we will use fs. readFile , passing it the path to our file, an optional encoding type, and a callback to receive the file data. If the file is successfully read, the contents will be passed to the callback.

How do I export json data from node JS?

nodejs-write-json-object-to-file.jsparse(jsonData); console. log(jsonObj); // stringify JSON Object var jsonContent = JSON. stringify(jsonObj); console. log(jsonContent); fs.

What is swig in Nodejs?

Swig is an awesome, Django/Jinja-like template engine for node. js.


1 Answers

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 [email protected]
npm WARN deprecated [email protected]: v1.0.0 is a complete rewrite of Swig
from the ground up. Previous versions are no longer supported
like image 179
Jonathan Lonowski Avatar answered Oct 22 '22 01:10

Jonathan Lonowski