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

Sharing client variable with Meteor.onCreateUser function on server

本文关键字:函数 共享 客户端 变量 onCreateUser Meteor 服务器      更新时间:2023-09-26

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

看起来你应该在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 }}
        );
    }
});

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