问题描述
我有代码
datas = new Meteor.Collection("datas")
var data = datas.findOne('101abcde1f2345ac00000001')
if (Meteor.is_client) {
Meteor.startup(function () {
console.log(data.name)
});
}
但是我在控制台中得到的是一个 undefined
错误.但是,如果我在网络检查器的 javascript 控制台中键入 console.log(data.name)
(大概经过某种等待它可以工作.我已经将代码放入 Meteor.startup 以确保DOM 已准备就绪.我可能做错了什么?
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?
推荐答案
您似乎正在使用 autopublish
包.而且(当然),Meteor.startup
不会等待订阅完成.
It seems that you are using autopublish
package. And (of course), Meteor.startup
doesn't wait subscription completed.
通常,我们使用 reactive context &数据在 Meteor 中执行此操作 -
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 )}
});
}
任何时候datas
集合有变化(?),Meteor.autosubscribe
中的函数都会被调用.
Anytime datas
collection has changes(?), the function in Meteor.autosubscribe
will be called.
这篇关于流星收集查询“未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!