简体   繁体   English

Mongo DB流星

[英]Mongo db meteor

I am new to mongo db and meteor. 我是mongo db和流星的新手。 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. 我想在我的客户端助手中检索属于该特定日历的所有calendarios_slaves的所有ID,以使用它们查询特定的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. 我设法检索了此日历的所有calendario_slave的所有ID,但是它们都在一行中,因此我实现了SPLIT以使用while循环拆分它们,但现在的问题是我无法在for之外访问SPLIT结果(myTrozo)循环,第一个console.log(myTrozo)显示我需要的内容,但我不知道如何管理它返回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. 使用.concat连接数组。 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;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM