Monday, 22 May 2017

bluebird node promises, cant resolve value

I'm trying to write some promises in my express application using bluebird, but have run into a problem. How do I access the user value here - it is not being returned by the resolve(response) method.

This is the routes file:

router.get('/auth/google/callback', (req, res) => {

    google.getToken(req.query.code)
        .then( () => { google.getUser() })
        .then(user => {
            console.log(user); //undefined - response is NOT defined here
        })
});

This is the file to manage authentication from which the methods are exported.

module.exports.getToken = (code)=> {
    console.log('step 1');
    return new Promise((resolve, reject) => {

        oauth2Client.getToken(code, (err, tokens) => {
            if (!err) {
                oauth2Client.setCredentials(tokens);
                resolve();
            }

        });

    });

};

module.exports.getUser = () => {
    console.log('step 2');
    return new Promise((resolve, reject) =>{

         plus.people.get({
            userId: 'me',
            auth: oauth2Client
        }, (err, response) => {
            if(!err)
                resolve(response); //response is defined here as the user
            else
                reject(err);
        });
    });
};



via userqwert

No comments:

Post a Comment