Meteor find使用变量返回null

时间:2016-01-05 10:32:56

标签: javascript meteor meteor-collections

问题描述:

我有两个集合videosspecsvideos集合有一个名为spec的密钥,对应于specs id。两个集合都不是空的。

我的模板助手:

  Template.List.helpers({
    videos: function(){
      var vids = Videos.find({ online: false}).fetch();
      return vids.map(function(value){
        console.log("id: " + value.spec);
        console.log(Specs.find({ id: value.spec }).fetch());

        //Issue here
        value.specName = Specs.find({ id: value.spec }).fetch()[0].name;
        return value;
      });
    }
  });

正如您在模板帮助器中看到的那样,我遍历视频阵列并将specName添加到数组中。

主要问题来自这条特定的路线:

value.specName = Specs.find({ id: value.spec }).fetch()[0].name;

,更具体地说,这一行:Specs.find({ id: value.spec }).fetch() 每次都会返回null

我尝试过的事情:

当然,我的第一个想法是检查value.spec返回的内容。它返回1到15之间的一个int(包含),这是正确的。如果value.spec是正确的,那么find()为什么不返回任何内容?

然后我决定努力设置它并尝试了这个:

Specs.find({ id: 2}).fetch()

这很有效。这很奇怪,因为value.spec多次返回2 ...

为了以防万一,我还试过了intParse(value.spec),但是这也没有用。

问题

为什么Specs.find({ id: value.spec }).fetch()返回null,因为知道value.spec设置正确并且硬编码的数字有效?

请求的json数据:(来自meteor mongo)

规格:

{ "_id" : "XKXHtQuiFsAew3dDy", "id" : 1, "name" : "Endocrine surgery" }
{ "_id" : "68jFidAMXTXpQtQye", "id" : 2, "name" : "General and digestive" }
{ "_id" : "GZSXToRXMfJgnH3CY", "id" : 3, "name" : "Pediatric surgery" }
{ "_id" : "T2mBz2gsXEqQaybmq", "id" : 4, "name" : "Thoracic surgery" }
{ "_id" : "hnuQzZiPKvYYDZhc8", "id" : 5, "name" : "Equipment" }
{ "_id" : "byE3A6HchvfhKdmR8", "id" : 6, "name" : "Gynecology" }
{ "_id" : "u5rrPB7asGW3NC6B2", "id" : 7, "name" : "Urology" }
{ "_id" : "umxKvR66oEx5dRppf", "id" : 8, "name" : "Cardiovascular surgery" }
{ "_id" : "bPcBTZn3t5ubRRcrQ", "id" : 9, "name" : "Endoscopic surgery" }
{ "_id" : "yNyAqQPoreNtdRZ34", "id" : 10, "name" : "NOTES" }
{ "_id" : "KG794eakRaztEqehG", "id" : 11, "name" : "Robotic surgery" }
{ "_id" : "QBrtvTg4GT7Tf7cAJ", "id" : 12, "name" : "Skull base surgery" }
{ "_id" : "HEhq6oBjuuMnrxE5a", "id" : 13, "name" : "Arthroscopy and upper limb surgery" }
{ "_id" : "xwpgHqZpBQP7WAnd5", "id" : 14, "name" : "Single port surgery" }
{ "_id" : "K4BgFupwNdDGD3449", "id" : 15, "name" : "Telemicrosurgery" }

视频:

{ "_id" : "L5Qi7YRRhn6Sfcjk8", "id" : "vd01en1065e", "title" : "Right inguinal hernia: open plug technique", "authors" : [ "E Pelissier" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false }
{ "_id" : "M8cuLW6KNCqKeP9vF", "id" : "vd01en1074e", "title" : "Laparoscopic splenectomy, posterior approach", "authors" : [ "D Mutter", " F Rubino" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false }
{ "_id" : "Ptzrxw8GifeMvQk9k", "id" : "vd01en1090e", "title" : "Intussusception of the intestine in the newborn", "authors" : [ "F Becmeur", " D Christmann", " I Kauffmann" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 3, "private" : true, "online" : false }
{ "_id" : "oHWcX3vCBHuZQM9hR", "id" : "vd01en1103e_2", "title" : "Appendicular peritonitis: laparoscopic conversion", "authors" : [ "B Navez" ], "date_published" : "2001-11-05", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false }
{ "_id" : "6uzmxYxhd5DDuS2gG", "id" : "vd01en1108e", "title" : "Diaphragmatic hernias", "authors" : [ "F Becmeur" ], "date_published" : "2001-11-28", "abstract" : "", "tags" : [ "" ], "spec" : 3, "private" : true, "online" : false }
{ "_id" : "yHqruiQYeeQ9SDHpH", "id" : "vd01en1112e", "title" : "Laparoscopic excision of the cystic stump", "authors" : [ "J Leroy" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false}
{ "_id" : "fmjtk5WAEKitMxyGj", "id" : "vd01en1114e", "title" : "Laparoscopic gastric banding in a patient with a BMI of 40", "authors" : [ "JM Zimmermann", " D Fölscher" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false}

我已经在这个问题上坚持了几个小时,我不想发布这个问题,因为我认为这是一个简单的问题。然而,这令人难以置信。

2 个答案:

答案 0 :(得分:1)

此处的问题是发布/订阅问题。你没有提到它,你如何处理出版物和订阅,但这很可能是问题。发生的事情是,当您浏览视频集时,哪个订阅准备就绪(因为您有任何数据):Videos.find({ online: false})这并不一定意味着(在那个精确的时刻)订阅处理Spec集合也准备好了。因此,即使在服务器上查询正在运行,在客户端上它也是空的,因为数据不会在客户端和服务器YET之间同步。所以你必须等到两个订阅都以某种方式准备好了。您可以在路由器中使用模板订阅或waitOn功能。

答案 1 :(得分:0)

首先在服务器端检查您的发布和订阅是否准备好并返回数据检查,如

 Meteor.publish('Videso', function () {

  var result= Videso.find({});
  console.log(result);
   return result
   });

如果console.log在服务器端返回你的记录我有一个解决方案然后你的  问题

问题的解决方案是

   Template.List.helpers({
   videos: function(){

    var vids = Videos.find({ online: false}).map(function (value) {
             console.log("id: " + value.spec);
             var idValue  = Specs.findOne({ "id": value.spec })
             console.log("idValue===",idValue)
             return _.extend(value, idValue);
       });
           console.log(vids);      //here merged data of both table having 
                                  // relvent record will returned
           return vids;



       }


  });

只需实施它并检查模板上的数据,它就会为您准备好并投票给我:)