2

私は 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

4

1 に答える 1

2

これは、ロギングの前に作成が呼び出され、ロギングが非同期であるためです..そのため、ifがtrue/falseになるかどうかを保証できません。これらの情報はすべてユーザーとともに保存されているため、fb サービスからの情報を入力するのは冗長です。

http://docs.meteor.com/#meteor_user

ユーザーがログインした後にユーザーに関する情報を取得する必要があります。その時点で、ユーザー名/IDを使用できる識別子の種類を認識できるためです。

//Server side
Meteor.publish("userData", function () {
    return Meteor.users.find({_id: this.userId});

    // You can publish only facebook id..
    /*return Meteor.users.find({_id: this.userId},
        {
            fields: {
                'services.facebook.id': true
            }
        }
    );*/
});

//Client side
Meteor.subscribe("userData");

// .. you can see more informations about logged user
console.log(Meteor.users.find({}).fetch());
于 2013-04-18T21:41:55.047 に答える