JavaScript How do I send on object from MongoDB to jade? send object from one activity to another,send object from

I'm trying to access an object that I retrieve from MongoDB with my client-side JS. Specifically, I want to be able to loop through and use the arrays within the object. Here is my server-side JS which successfully finds the results and logs them to terminal.

app.get("/post/:id", function (req, res, next) {
  var id = req.param('id');
  var query = BlogPost.findById(id).populate('author');

  BlogPost.find(query, {"answers":"true", "blanks":"true", "_id":0}, function(err, results){

  console.log('Results '+results);//This prints an object like:// { answers: [ 'more', 'One', 'more' ], blanks: ['try']}

  query.exec(function (err, post) {
    if (err) return next(err);
    if (!post) return next(); // 404

    res.render('post/view.jade', { post: post, results: results });
  })
})
})

And my jade:

  #luck
   #{results}

And then my client-side JS:

var results = $('#luck').html();
console.log(results.answers[2]);

I get undefined with the console.log and the results are printed on the page.

Answer:1

If the results are printed on the screen but you cannot traverse the object, then it may mean a few things:

1) The object might be an array i.e. results[0].whatever instead of results.whatever

2) The object is in string form and you need to call JSON.parse(results) before you can traverse it

var results = JSON.parse($('#luck').html());
console.log(results.answers[2]);
Answer:2

I want to update a clock in the UI when the time changes from 12:01 to 12:02. I can do a setInterval every 60 seconds, but the beginning might not be on the first second of a new minute. How can I ...

I want to update a clock in the UI when the time changes from 12:01 to 12:02. I can do a setInterval every 60 seconds, but the beginning might not be on the first second of a new minute. How can I ...

Using the following code: params = { Bucket: 'bucket.com', Key: zipPath }, s3 = new AWS.S3(); params.Body = zipFile; s3.putObject(params, function(err, data) { if (err) { ...

Using the following code: params = { Bucket: 'bucket.com', Key: zipPath }, s3 = new AWS.S3(); params.Body = zipFile; s3.putObject(params, function(err, data) { if (err) { ...

  1. upload file amazon s3 javascript
  2. upload file amazon s3 java
  3. upload file amazon s3 android
  4. upload file amazon s3 c#
  5. upload file amazon s3
  6. upload file amazon s3 command line
  7. upload file amazon s3 using php
  8. upload file amazon cloud drive
  9. upload amazon file
  10. upload file to amazon s3 codeigniter
  11. upload file to amazon s3 node js
  12. upload file to amazon workspace
  13. upload file to amazon s3 laravel
  14. upload file to amazon s3 bucket c#
  15. upload file to amazon s3 php
  16. upload file to amazon ec2
  17. upload file to amazon ec2 instance
  18. upload file to amazon s3 python
  19. upload flat file amazon
  20. upload file to amazon kindle

I am starting to use Ractive and I would like to understand better how to make reusable components. It is quite easy to write a generic widget that can be used in different situations. My problem is ...

I am starting to use Ractive and I would like to understand better how to make reusable components. It is quite easy to write a generic widget that can be used in different situations. My problem is ...

I'm trying to write a lightbox that shows an image inside a container when a div is being clicked. Said div has the actual image to be displayed as a background image, working as a thumbnail. These ...

I'm trying to write a lightbox that shows an image inside a container when a div is being clicked. Said div has the actual image to be displayed as a background image, working as a thumbnail. These ...

  1. getting client side value in code behind