【问题标题】:Meteor find is returning null with variable [duplicate]Meteor find返回带有变量的null [重复]
【发布时间】:2016-01-06 13:26:21
【问题描述】:

问题描述:

我有两个集合 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)以防万一,但也没有用。

问题

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

请求的 json 数据:(来自流星 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}

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

【问题讨论】:

  • 您可以发布一些您正在使用的数据(JSON 格式)吗?
  • 添加了@chrisklaussner。

标签: javascript meteor meteor-collections


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2018-03-02
      • 2019-09-21
      相关资源
      最近更新 更多