Answer the question
In order to leave comments, you need to log in
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
}
}
}
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;
}
});
{{#each tests}}
{{number}}
{{name}}
{{lastname}}
{{/each}}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question