S
S
Sergo Sergo2015-04-20 17:25:48
MongoDB
Sergo Sergo, 2015-04-20 17:25:48

How to edit and display a collection?

I am writing a site on Meteor.js, there was a problem. You need to take the data from the collection, edit it and display it on the page. View collection:

{ 
    "_id": "WEqesdweWEWAEe", 
    number: 1, 
    other: { 
        '514': { 
            test: 1, 
            test2: 2
        }
    }
}

I get this data and edit it:
Template.test.helpers({
tests: function() {
      testres = [];
      test = Test.find({number: 1});
      test.forEach(function(doc){
        for(i in doc.other) {
          testres[i] = {
            name: doc.other[i].test,
            lastname: doc.other[i].test2,
            number: doc.number
          };
        }
      });
    return testres;
}
});

Then I want to take them out
{{#each tests}}
    {{number}}
    {{name}}
    {{lastname}}
{{/each}}

But it doesn't output anything, what should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Shatokhin, 2015-04-20
@sergo_serga

The call to mongo is asynchronous . Here's a function to get data, but it won't make a synchronous render helper. Get the data first, then render it.

function getTesters (cb) {
  var testres = [];
  Test.find({number: 1}).toArray(function (err, test) {
    test.forEach(function(doc){
      for(var i in doc.other) { // "for in" очень плохое решение в плане скорости, я бы заменил на var keys = Object.keys(doc.other); for(var i = 0; i < keys.length; i++) { testres[keys[i]] = /* код*/ }
        testres[i] = {
          name: doc.other[i].test,
          lastname: doc.other[i].test2,
          number: doc.number
        };
        cb(err, testres);
      }
    });
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question