【问题标题】:Sharing client variable with Meteor.onCreateUser function on server与服务器上的 Meteor.onCreateUser 函数共享客户端变量
【发布时间】:2016-01-18 23:57:22
【问题描述】:

我想与服务器上的 Meteor.onCreateUser 函数调用共享客户端中设置的变量。

我有这段代码可以在创建用户之前设置一些用户属性

Accounts.onCreateUser(function(options, user, err) {

    if (options.profile) {

      user.profile = options.profile;

      // Images
      var picturelrg = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large";
      var picturesm = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=small";

      options.profile.picturelrg = picturelrg;
      options.profile.picturesm = picturesm;
      options.profile.upvotes = 0;
      options.profile.neutralvotes =  0;
      options.profile.downvotes = 0;
      // ip = response.ip;


      return user;
    }
 });

这是客户端代码

if (Meteor.isClient) {

fbLogin = function() {
    Meteor.loginWithFacebook({
        requestPermissions: ['public_profile', 'email', 'user_location']
    }, function(err) {
        if (err)
        // redirect to register if popup comes and user isn't on register
            Session.set('errorMessage', err.reason || 'Unknown Eror');
        console.log(Session.get('errorMessage'));
    });
}


locate = function(){

    function ipLocate(whenDone) {
      var api = "http://ipinfo.io?callback=?";
      $.getJSON(api, {
          format: "jsonp"
        })
        .done(function(response) {
          var result = ""

          // show all the props returned
          for (var prop in response) {
            result += prop + ": " + response[prop] + "<br>";
          }

          var selectedResponse = {
            city: response.city,
            region: response.region,
            country: response.country,
            ip: response.ip,
            latLng: response.loc
          }
          console.log(selectedResponse);
          whenDone(selectedResponse);

          return selectedResponse
        });
    }

    // HACK: Async
    function ipDone(selectedResponse) {
      response = selectedResponse;
    }

    // Set response
    ipLocate(ipDone);
    return response
}

Template.ModalJoin.events({
    'click .modJoinFB-Btn ': function() {
    locate();
    fbLogin();
    }
});

}

在客户端上,我有一个事件处理程序,当用户单击“使用 Facebook 注册”按钮时,它会设置一些值。如何将这些值发送到要访问的 onCreateUser 函数。

例如:我想在用户注册时存储用户地理位置信息(城市、州),但我不知道如何将其从客户端发送到服务器。

如果可以的话,我不确定如何使用 Meteor.call()

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    看起来您应该在fbLogin 中运行Meteor.call 函数,如果没有返回错误,则传递该位置数据。像这样的:

    fbLogin = function() {
        Meteor.loginWithFacebook({
            requestPermissions: ['public_profile', 'email', 'user_location']
        }, function(err) {
            if (err) {
                Session.set('errorMessage', err.reason || 'Unknown Eror');
                console.log(Session.get('errorMessage'));
            } else {
                //if no error was returned, then Meteor.call the location
                var userId = Meteor.userId(); //you should send that userId for the method.
                Meteor.call('storeLocation', locationData, userId, function(err,res){
                    if (err) {
                        console.log(err);
                    }
                });
            }
        });
    }
    

    在服务器上,您创建了一个方法来使用该位置更新该用户配置文件数据。也许是这样的:

    Meteor.methods({
        'storeLocation': function(locationData, userId) {
            var locationData = {
                // based on what you have gathered on your client location function
                'city': response.city,
                'region': response.region,
                'country': response.country,
                'ip': response.ip,
                'latLng': response.loc
            }
            Meteor.users.update(
                //I suggest placing it inside profile, but do as it is better to you
                {'_id': userId},
                {$addToSet: {'profile.locations': locationData }}
            );
        }
    });
    

    不确定您是否会这样存储,但这就是我为自己所做的。如果有任何问题或疑问,请告诉我,我们可以一起尝试解决。

    【讨论】:

    • 我曾考虑过这一点,但我认为 Meteor.users.update() 可能会在用户创建之前运行到早期。我很快就会知道这是否有效。
    • 如果它不起作用,请告诉我。我想它会因为它是在服务器从 facebook 登录检索成功或错误之后触发的。根据我的经验,在 Meteor.call 或任何其他服务器调用中嵌套函数通常可以按预期工作。如果它不在 Meteor.loginWithFacebook 之外,那么您评论的就是这种情况。但由于它是嵌套的,取决于成功,它可能会起作用。
    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多