[英]How do I pass a variable from server side to client side js Meteor
我正在使用外部API。 我想显示登录用户的昵称(Steam API)
服务器main.js
Meteor.startup(function () {
ServiceConfiguration.configurations.upsert(
{ service: 'steam' },
{
$set: {
loginStyle: 'redirect',
timeout: 10000 // 10 seconds
}
}
);
Accounts.onLogin(function() {
var steam64Id = Meteor.user().profile.id;
console.log(steam64Id + " is logged in.");
// To retrieve more details about user
var steamApiKey = ("XXXXXXXXXXXXX");
var result = Meteor.http.get('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxx&steamids=76561197977045111');
console.log(result.data.response.players[0].personaname);
});
});
这会在控制台日志中返回“我的昵称”,但我想在客户端检索此变量,因此我可以向客户端js模板添加{{Nickname}}并在模板中显示用户的昵称
最明显的方法是将结果添加到用户记录中的用户个人资料中。 然后,它将自动同步到客户端,并可以在那里使用。
var steamApiKey = ("XXXXXXXXXXXXX");
var result = Meteor.http.get('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxx&steamids=76561197977045111');
Meteor.users.update(Meteor.userId(),
{ $set: { 'profile.personaname': result.data.response.players[0].personaname }}
);
在客户端,使自己成为一个助手(全局助手或特定于您需要的模板的助手)以获取此值:
Nickname(){
return Meteor.user().profile.personaname;
}
请注意,用户下的配置文件对象会自动发布到客户端,因此您不必为此进行特殊发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.