繁体   English   中英

与服务器上的Meteor.onCreateUser函数共享客户端变量

[英]Sharing client variable with Meteor.onCreateUser function on server

我想与服务器上的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()

看起来你应该运行Meteor.call内部功能fbLogin ,经过该位置的数据,如果没有返回错误。 像这样:

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

不知道您是否会那样存储,但这就是我为自己所做的。 让我知道是否有任何问题或疑问,我们可以尝试一起解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM