I am new to mongo db and meteor. I have a document as the one below: Calender:
{
"_id" : ObjectId("577a09d3e9ac22d62a20ab01"),
"status_visualizacion" : "visible",
"status_tipo" : "Pintura",
"createdAt" : ISODate("2016-07-04T07:01:39.018Z"),
"usuarios_admin" : [
ObjectId("5773976c201bb491f499c180"),
ObjectId("577a03db9da98306f624c3d9"),
ObjectId("577a041d9da98306f624c3da"),
ObjectId("577a07b7e9ac22d62a20aae9"),
ObjectId("577a07c6e9ac22d62a20aaea"),
"Ys6fiychXcSfCgWox"
],
"grupo_usuarios" : [
ObjectId("5773976c201bb491f499c180"),
ObjectId("577a03db9da98306f624c3d9"),
ObjectId("577a041d9da98306f624c3da"),
ObjectId("577a07b7e9ac22d62a20aae9"),
ObjectId("577a07c6e9ac22d62a20aaea")
],
"calendario_slaves" : [
ObjectId("577b6a0114b9512e1e3f4c10"),
ObjectId("577b6a1d14b9512e1e3f4c11"),
ObjectId("577b6a2414b9512e1e3f4c12")
]
}
I want to retrieve all the Id's of all the calendarios_slaves that belongs to this particular Calender in my client side helper in other to use them to query for a particular calendarios_slave. I have tried all I could but all to no result. The code I have presently is this:
Template.testeo.helpers({
ls: function(){
var list=Calender.find({status_visualizacion: "visible"});
var result="";
list.forEach(function(calender){
result += calender.calendario_slaves + " ";
});
console.log(result);
console.log("split");
mySplitResult = result.split(",");
for ( var i = 0; i < mySplitResult.length; i++ ) {
mySplitResult2 =mySplitResult[i].split(" ")
for ( var j = 0; j < mySplitResult2.length; j++ ) {
myTrozo= mySplitResult2[j];
console.log(myTrozo);
}
}
//console.log(myTrozo);
return myTrozo;
}
});
I managed to retrieve all the Id's of all the calendario_slave of this Calender butthey were all in a single line so I implemented the SPLIT to split them using a while loop but the problem now is that I cant access SPLIT result (myTrozo) outside the for loop, the first console.log(myTrozo) displays what i need but I don't know how to manage it return myTrozo. Could any one with more experience help me out if there's anything am doing wrong. Thanks
var items = Meteor.subscribe('Calendar');
var itemsCursor = items.find({ status_visualizacion: "visible" });
while ( itemCursor.hasNext() ) {
item = itemCursor.next();
for (i = 0; i < item.calendario_slaves.length; i++) {
console.log(item.calendario_slaves[i]);
}
}
Use .concat
to concatenate arrays. You don't need to convert to strings and split.
Template.testeo.helpers({
ls() {
const result=[];
Calender.find({status_visualizacion: "visible"}).forEach(e => {
if ( e.calendario_slaves && e.calendario.slaves.length ){ // guard against missing/empty
results.concat(e.calendario_slaves);
}
});
return result;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.