Justin
Justin

Reputation: 65

Making JSON code 'cleanly displayed' on an HTML page

I'm trying to get this code cleaner, it does display on the html page.

app.get("/web/users", function(req, res) {
var users = app.models.users.model('User');
users.find({}).lean().exec(function(err, result){
res.write(JSON.stringify(result));
res.end();

This does work, and it lists something like this...

[{"_id":"...","name":"Justin","phonenumber":"5555555555","useSMS":true,"callerId":["..."],"PIN":"..."}]

Now, I would like this code to display like this...

Justin 5555555555

Upvotes: 1

Views: 93

Answers (1)

salezica
salezica

Reputation: 77039

If I understood correctly and you want your json pretty-printed, here:

res.write(JSON.stringify(result, null, 2));

That will indent your JSON hierarchically using 2 spaces.

EDIT: re-reading with Mouseroot's comment, I think what you actually want is:

res.write(result.name + ' ' + result.phonenumber);

Upvotes: 2

Related Questions