Monday, 8 May 2017

AJAX call for Node/Express to render aPolymer iron-list

My goal: to render an <iron-list> by ajax call to Node.

Firstly, codes:

index.handlebars

<iron-ajax url="./allusers" last-response="" auto></iron-ajax>
<iron-list items="" as="item">
   <template>
      <div>
         Name: 
      </div>
   </template>
</iron-list>

app.js

app.get('/allusers', function(req, res){
  funct.getAllUsers()
    .then(function (allusers) {
      var data = [];
      allusers.forEach(function(eachuser) {
        data.push(JSON.stringify(eachuser));
      });
      res.send(data);
    })
    .catch(function (err){
      console.log("Error at getAllUsers: " + err);
    });
});

Here getAllUsers() is a mongodb function which returns an array by

collection.find().toArray()

And allusers should already be an array of JSON but with some formatting issue so I called JSON.stringify to make sure each element is a properly formatted JSON object. But I still get this error:

Polymer::Attributes: couldn`t decode Array as JSON

And my <iron-list> is always empty. Why?



via tic30

No comments:

Post a Comment