Meteor onCreateUser问题

Meteor onCreateUser问题,meteor,Meteor,我正在使用Meteor,人们可以通过Facebook连接到该网站。我用人的用户名来识别他们。但是,其中一些没有用户名。例如,新用户的用户名为null。我想做的是,如果这个人有用户名,那么我们就使用他们的用户名。如果没有,我想使用他们的Facebook id作为用户名。问题是,如果我的条件不能正常工作。如果此人有用户名,则If条件认为此人没有用户名。奇怪的是,如果我在if条件之前对用户名执行console.log,它将显示用户名。但是一旦在if中,它就会认为用户名为null。代码如下: Accou

我正在使用Meteor,人们可以通过Facebook连接到该网站。我用人的用户名来识别他们。但是,其中一些没有用户名。例如,新用户的用户名为null。我想做的是,如果这个人有用户名,那么我们就使用他们的用户名。如果没有,我想使用他们的Facebook id作为用户名。问题是,如果我的条件不能正常工作。如果此人有用户名,则If条件认为此人没有用户名。奇怪的是,如果我在if条件之前对用户名执行console.log,它将显示用户名。但是一旦在if中,它就会认为用户名为null。代码如下:

Accounts.onCreateUser(function(options, user) {
  var fb = user.services.facebook;
  var token = user.services.facebook.accessToken;

    if (options.profile) { 

        options.profile.fb_id = fb.id;
        options.profile.gender = fb.gender;
        options.profile.username = fb.username    

        console.log( 'username : ' + options.profile.username); 


        if ( !(options.profile.username === null || options.profile.username ==="null" || options.profile.username === undefined || options.profile.username === "undefined")) {
          console.log('noooooooo');
          options.profile.username = fb.id; 
        } else {
          console.log('yessssssss');
          options.profile.username = fb.username;
        }

        options.profile.email = fb.email; 
        options.profile.firstname = fb.first_name;

        user.profile = options.profile;     
    }


    sendWelcomeEmail(options.profile.name, options.profile.email); 
    return user;
}); 

使用此代码,如果我使用带有用户名的Facebook登录。该条件将显示“nooooo”,但显示console.log('username:'+options.profile.username);将显示我的用户名。为什么会这样l

这是因为在记录之前调用创建,而记录是异步的。。因此,您无法确保您的if是否为真/假。您从fb服务输入的信息是多余的,因为所有这些信息都已与用户一起保存

您应该在用户登录后获得有关他的信息,因为在那一刻,您将能够识别您可以使用用户名/id的标识符

//服务器端
Meteor.publish(“userData”,函数(){
返回Meteor.users.find({u-id:this.userId});
//您只能发布facebook id。。
/*返回Meteor.users.find({u-id:this.userId},
{
字段:{
“services.facebook.id”:true
}
}
);*/
});
//客户端
Meteor.subscribe(“用户数据”);
// .. 您可以查看有关已登录用户的更多信息
log(Meteor.users.find({}.fetch());

这正是我在服务器端和客户端的功能。但它不起作用。你能创建例如Gist并给我链接吗?我能为你提供更多帮助吗?我的问题略有不同,但在尝试了将近8个小时的其他想法后,这个终于起作用了。直到我开始提问,我才找到它。谢谢@janrudovsky如果
语句中的条件被
否定,那么您的
语句中的条件将被否定。运行输入和输出:假设用户名为“brad”。然后,条件中的
options.profile.username
将不等于null、null、undefined或undefined,因此内部条件将返回false。但是,它随后被
否定,因此您的条件语句的行为(至少大部分情况下)与您想要的相反。我还建议更改内部条件语句。检查JavaScript中是否存在变量的一种标准方法是通过
typeof options.profile.username!==“未定义”
。我想这可能对你有用。谢谢你,伙计!成功了:)