如何在此代码中将服务器发送给客户'''用户'''?

时间:2018-03-06 12:36:36

标签: javascript node.js typescript

我正在使用此代码获取Facebook用户个人资料,但我无法向客户端发送此用户变量。我该怎么办?

Dahaneleryazayımderdimbukadareşte。 Facebookkullanıcıbilgileriniclienttarafınadönemedim。 Neyapmalıyım?

这是我的代码:

router.post('/fbLogin', async function (ctx, next) {

    var user;
    console.log(ctx.request.body.token);
    var options = {
        host: 'graph.facebook.com',
        port: 443,
        path: '/v2.11/me?fields=id,first_name,last_name,email',
        method: 'GET',
        headers: { 'Authorization': 'Bearer ' + ctx.request.body.token }
    };

    const req = https.request(options, async function (res) {

        res.setEncoding('utf8');
        res.on('data', async function (d) {
            var fbJsonObject = JSON.parse(d);
            user = await db.collection('users').findOne({ facebookId: fbJsonObject.id });

            if (!user) {
                var uname = fbJsonObject.first_name.toString() + '_' + fbJsonObject.last_name.toString().substring(0, 1);
                console.log(uname);
                var count = await db.collection('users').find({ username: /uname/ }).count();
                if (count != 0) {
                    uname = uname + count.toString();
                }

                var pass = customPassword();
                await db.collection('users').insertOne({
                    method: 'facebook',
                    email: fbJsonObject.email,
                    username: uname,
                    password: phash.generate(pass),
                    friends: [],
                    facebookId: fbJsonObject.id
                });

                user = await db.collection('users').findOne({ facebookId: fbJsonObject.id });
                console.log("Kayıt başarılı");
                console.log(user);
            }
        })
    });
    req.end();
});

和客户端代码:

async loginWithFB(){

await this.facebook.login(['email', 'public_profile']).then((response: FacebookLoginResponse) => {
  this.facebook.api('me?fields=id,name,email,first_name,last_name,picture.width(720).height(720).as(picture_large)', []).then(profile => {
    //this.userData = { id: profile['id'] }
    this.fb(response.authResponse.accessToken);
  })


})

}

async fb(token2){

var resp = await this.http.post('http://api.suma.nopsmart.com:3000/fbLogin', {
  token: token2
}).toPromise();
var x = resp.json();
console.log(x);

}

0 个答案:

没有答案