1

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

2
  • now you just need to treat your json string as an object eg: result.name and result.phonenumber Commented Feb 24, 2014 at 21:36
  • @Mouseroot oh, I think I misunderstood the question Commented Feb 24, 2014 at 21:42

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

yea alsong as he uses result = JSON.stringify(result); he can treat the JSON string as an object, if your in chrome you could do a console.dir(result) to get an even better view of how everything is structured for more complex JSON objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.