如何将变量从服务器端传递到客户端js Meteor



Im使用外部API。我想显示登录用户的昵称(蒸汽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);  
    });
  });

这会在控制台日志中返回"我的昵称",但我想在客户端检索这个变量,这样我就可以将{{Nickname}}添加到客户端js模板中,并在模板中显示用户的昵称

最明显的方法是将结果添加到用户记录中的用户配置文件中。然后,它将自动同步到客户端,并可在那里使用。

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;
}

请注意,用户下的概要文件对象会自动发布到客户端,因此您不必为其制作特殊的发布。

最新更新