Meteor:访问客户端上的用户详细信息
Meteor: accessing user details on client
我正在尝试在 meteor 的客户端访问(另一个)用户的详细信息。我有一个名为 'userDetails' 的服务器端方法,我从一个名为 'acc' 的模板助手调用它。
服务器方法:
'userDetails': function(userId) {
check(userId, String);
return Meteor.users.findOne({_id: userId},
{fields: {
"services.facebook.first_name": 1,
"profile.birthday": 1,
"services.facebook.gender": 1,
"profile.location.name": 1
}});
}
模板助手:
acc: function(_id) {
Meteor.call('userDetails', _id, function(err, res) {
if(err) throw error;
return res;
});
}
当我尝试访问模板中的 acc.profile.birthday 时,我什么也没得到。这可能是什么原因造成的?
您必须将 return 包裹在 else 语句中。
if(error) {
}
else {
return res;
}
异步调用你的方法。这意味着回调函数将在您的服务器方法完成时执行。
如果要在模板上显示结果,有两种可能:
1/ 使用会话。
acc: function(_id) {
Meteor.call('userDetails', _id, function(err, res) {
if(err){
}else{
Session.set('data', res)
}
});
return Session.get('data')
}
2/ 使用模板订阅(更好的解决方案):
在服务器上,您发布数据:
Meteor.publish("data", function(){
return Meteor.users.findOne(...)
});
在客户端,您订阅:
Template.mytemplate.onCreated(function () {
Template.instance().subscribe("data");
});
然后直接在客户端上您将能够创建一个助手并调用 findOne。
在html:
{{#if Template.subscriptionsReady}}
{{#each myHelper}}
{{acc.profile.birthday}}
{{/each}}
{{else}}
<p>Loading...</p>
{{/if}}
关于用户的重要通知:
默认情况下,用户配置文件是可编辑的。请阅读:https://dweldon.silvrback.com/common-mistakes
Meteor 调用是异步调用,这就是您的助手不返回任何数据的原因。
此处的最佳选择是使用 Session
或 ReactiveVar
或 ReactiveDict
我将在此处使用 Session
选项
acc: function(_id) {
Meteor.call('userDetails', _id, function(err, res) {
if(err){
}else{
Session.set('userDetails', res)
}
});
return Session.get('userDetails')
}
在您的 html 中,您可以像这样使用这个助手
{{#if acc}}
{{name}}
...
{{else}}
<p>Information not found</p>
{{/if}}
我正在尝试在 meteor 的客户端访问(另一个)用户的详细信息。我有一个名为 'userDetails' 的服务器端方法,我从一个名为 'acc' 的模板助手调用它。
服务器方法:
'userDetails': function(userId) {
check(userId, String);
return Meteor.users.findOne({_id: userId},
{fields: {
"services.facebook.first_name": 1,
"profile.birthday": 1,
"services.facebook.gender": 1,
"profile.location.name": 1
}});
}
模板助手:
acc: function(_id) {
Meteor.call('userDetails', _id, function(err, res) {
if(err) throw error;
return res;
});
}
当我尝试访问模板中的 acc.profile.birthday 时,我什么也没得到。这可能是什么原因造成的?
您必须将 return 包裹在 else 语句中。
if(error) {
}
else {
return res;
}
异步调用你的方法。这意味着回调函数将在您的服务器方法完成时执行。
如果要在模板上显示结果,有两种可能:
1/ 使用会话。
acc: function(_id) {
Meteor.call('userDetails', _id, function(err, res) {
if(err){
}else{
Session.set('data', res)
}
});
return Session.get('data')
}
2/ 使用模板订阅(更好的解决方案): 在服务器上,您发布数据:
Meteor.publish("data", function(){
return Meteor.users.findOne(...)
});
在客户端,您订阅:
Template.mytemplate.onCreated(function () {
Template.instance().subscribe("data");
});
然后直接在客户端上您将能够创建一个助手并调用 findOne。
在html:
{{#if Template.subscriptionsReady}}
{{#each myHelper}}
{{acc.profile.birthday}}
{{/each}}
{{else}}
<p>Loading...</p>
{{/if}}
关于用户的重要通知: 默认情况下,用户配置文件是可编辑的。请阅读:https://dweldon.silvrback.com/common-mistakes
Meteor 调用是异步调用,这就是您的助手不返回任何数据的原因。
此处的最佳选择是使用 Session
或 ReactiveVar
或 ReactiveDict
我将在此处使用 Session
选项
acc: function(_id) {
Meteor.call('userDetails', _id, function(err, res) {
if(err){
}else{
Session.set('userDetails', res)
}
});
return Session.get('userDetails')
}
在您的 html 中,您可以像这样使用这个助手
{{#if acc}}
{{name}}
...
{{else}}
<p>Information not found</p>
{{/if}}