私は Meteor を使用しており、人々は Facebook 経由でサイトに接続できます。私は人々のユーザー名を使用してそれらを識別しています。しかし、それらのいくつかはユーザー名を持っていません。たとえば、新しいユーザーのユーザー名は null です。私がやろうとしているのは、その人がユーザー名を持っている場合は、そのユーザー名を使用することです。そうでない場合は、Facebook ID をユーザー名として使用したいと思います。問題は、if 条件が正しく機能していないことです。その人がユーザー名を持っている場合、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 でログインするとします。条件には「noooooooo」が表示されますが、console.log( 'username : ' + options.profile.username); 私のユーザー名が表示されます。なぜそれをするのですか?:l