How can I use JSON.parse in nunjucks

Viewed 1607

i am get data in mongodb and express return occurs without errors but I wanted to use JSON.parse in the final results of my .find see below how I'm trying to do this

  app.get("/login", (req, res) => {
    var credentialClient = {
      expire_at: false,
      __v: false,
      _id: false
    };

    rememberMe.find({ username: "test-login" }, credentialClient, function (err, credentialInfo) {
      if (err) {
        res.send(err);
      } else {
        res.render("login.html", {
          usernameClient: JSON.parse(credentialInfo)
        });
      }
    });
  });

Without JSON.parse the final rendering stays that way in my login.html

{ username: 'test-login' }

The final results appear in login.html

<p class="center-align black-text" id="preFinalCredentialClient">{{ usernameClient }}</p>

Thanks for helping me!

3 Answers

It already returns JSON response so You can use this:

res.render("login.html", {
   usernameClient: credentialInfo.username
});
Related