0

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

2 Answers 2

1
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]);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.