Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从template.onRendered访问Meteor.users_Meteor - Fatal编程技术网

从template.onRendered访问Meteor.users

从template.onRendered访问Meteor.users,meteor,Meteor,我需要执行这段代码 Template.addPost.onRendered -> @$('#name').val(Meteor.user().profile.name) 但是,在客户端上填充Users集合之前,似乎会呈现模板。于是我回来了 Cannot read property 'profile' of undefined 如何解决此问题?您可以等待通过以下方式加载用户: Template.addPost.onRendered-> @自动运行(c)-> #以安全的方式提取名称 {na

我需要执行这段代码

Template.addPost.onRendered ->
@$('#name').val(Meteor.user().profile.name)
但是,在客户端上填充
Users集合
之前,似乎会呈现模板。于是我回来了

Cannot read property 'profile' of undefined

如何解决此问题?

您可以等待通过以下方式加载用户:

Template.addPost.onRendered->
@自动运行(c)->
#以安全的方式提取名称
{name}=Meteor.user()?.profile
#找到名称后,更新DOM并停止自动运行
如果名称
#请注意,名称是一个id,因此我们不需要@$
$('#name').val name
c、 停止()
我们还利用coffeescript的存在运算符安全地提取名称值。在javascript中,此解决方案如下所示:

Template.addPost.onRendered(函数(){
自动运行(函数(c){
var user=Meteor.user()
var name=user&&user.profile&&user.profile.name;
如果(姓名){
$('#name').val(name);
c、 停止();
}
});
});

很有效,非常感谢)你能详细说明两件事吗:1.大括号的卷曲符号是什么-
{name}
?;2.为什么jquery不需要@美元?假设我需要引用模板..请查看中的“Destructuring Assignment”
{x}=y
x=y.x
的缩写。您不需要在模板内搜索,因为
#name
在页面上只能存在一次(它是一个id),所以在整个页面内搜索将产生相同的结果。但是遍历模板不是比遍历整个页面更快吗?我想这与我的情况无关,但理论上…哇!你是大卫·韦尔登!伙计,这些好东西真是太好了!我不确定性能上的差异,但这并不重要,除非您拥有数量荒谬的DOM元素。是的,就是我——很高兴你喜欢这个博客D