与服务器上的 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()
看起来你应该在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 }}
);
}
});
不确定你是否会那样存储,但这就是我为自己所做的。如果有什么问题或疑问,请告诉我,我们可以尝试一起解决。
相关文章:
- 使用闭包共享构造函数参数
- Javascript创建函数,以便在其他函数之间共享变量
- AngularJS:如何与其他控制器共享作用域函数和变量
- 具有不同函数调用定义的共享JavaScript文件
- 节点模块对象范围:在所有函数之间共享一个对象
- 如何在不污染全局命名空间的情况下共享常用的jquery函数
- 如何在递归函数中共享返回值
- 在Javascript中的函数之间共享变量
- AngularJS:在控制器之间共享一个函数
- 嵌套函数中的 JavaScript 实例共享
- JavaScript Raphaël,多个数组共享相同的函数
- 如何在 Crockford 的新构造函数模式中共享“构造函数”功能
- 共享函数的 Javascript 类
- JavaScript - 如何注入不同的函数实现,例如 alert(与 web 和 Node 共享模块.js服务器端)
- 与服务器上的 Meteor.onCreateUser 函数共享客户端变量
- 改进这个自定义函数addClassIfVarDefined()或共享更好的解决方案
- 2 .函数共享同一个ajax函数
- 如何与一些javascript函数共享Java代码中的变量
- 与onclick函数共享Angular表达式
- 在javascript对象中,执行"static"函数和原型函数共享相同的命名空间