Meteor collection query 'undefined'

无人久伴 提交于 2020-01-13 06:42:08

问题


I have the code

datas = new Meteor.Collection("datas")
var data = datas.findOne('101abcde1f2345ac00000001')


if (Meteor.is_client) {
    Meteor.startup(function () {
        console.log(data.name)
    });
}

But what I get in the console is an undefined error. However if I type console.log(data.name) into the web inspector's javascript console (presumably after some kind of wait it works. I'm already putting the code in Meteor.startup to ensure that the DOM is ready. what could I be doing wrong?


回答1:


It seems that you are using autopublish package. And (of course), Meteor.startup doesn't wait subscription completed.

Usually, we use reactive context & data to do this in Meteor -

datas = new Meteor.Collection("datas")

if (Meteor.is_client){
  Meteor.autosubscribe(function(){
    var data = datas.findOne('101abcde1f2345ac00000001');
    if (data){ console.log( data.name )}
  });
}

Anytime datas collection has changes(?), the function in Meteor.autosubscribe will be called.



来源:https://stackoverflow.com/questions/11978663/meteor-collection-query-undefined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!