Express:为什么我无法在一个方法中访问会话变量而我可以在另一个方法中访问它?

时间:2018-01-12 09:46:28

标签: node.js express node-modules express-session

我正在使用googleapis依赖项来获取使用google + api的用户详细信息。当来自Google (app.get(/auth/google/callback))的重定向时,我会在会话中存储access_token。但是当我尝试在路线(/details)上访问它时,我无法实现它。 两条路线的代码是 -

对于谷歌重定向路线 -

app.get('/auth/google/callback', (req, res) => {
    let code = req.query.code;
    var session = req.session;
    oauthClient.getToken(code, (err, token) => {
        if(!err){
            //res.json(token)
            session['token'] = token;
            oauthClient.credentials = token;
            console.log('session ', req.session.token.access_token); // <------------ is accessible
            res.redirect('/details');
        }
        else{
            res.json({
                Error: 'Authorization error',
            });
        }
    });

})

详细路线 -

app.get('/details', (req, res, next) => {
    console.log(req.session.token.access_token); // <-------------cannot access req.session.token.access_token
    plus.people.get({
        userId: 'me',
        auth: oauthClient
    }, (err, userInfo) => {
        if(err){
            console.log("Error while fetching user info");
        }
        else{
            res.json(userInfo);   
        }
    });
});

这是错误 -

TypeError: Cannot read property 'access_token' of undefined
    at app.get (C:\Users\shahaji.shinde\Desktop\practice\google-signin\app.js:49:35)
    at Layer.handle [as handle_request] (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express\lib\router\index.js:275:10)
    at Immediate._onImmediate (C:\Users\shahaji.shinde\Desktop\practice\google-signin\node_modules\express-session\index.js:489:7)
    at runCallback (timers.js:793:20)

整个app.js -

const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const oauthClient = require('./oauthclient');
const google = require('googleapis');
const plus = google.plus('v1');
const config = require('./config');
const port  = 3000;
const app = express();

app.use(bodyParser.json());

//Session 
app.use(session({
    secret: config.session.secret,
    resave: true,
    saveUninitialized: true 
}));

app.get('/auth/google/callback', (req, res) => {
    let code = req.query.code;
    var session = req.session;
    oauthClient.getToken(code, (err, token) => {
        if(!err){
            //res.json(token)
            session['token'] = token;
            oauthClient.credentials = token;
            console.log('session ', req.session.token.access_token);
            res.redirect('/details');
        }
        else{
            res.json({
                Error: 'Authorization error',
            });
        }
    });

})

app.get('/login', (req, res) => {
    let url = oauthClient.generateAuthUrl({
        access_type: 'offline',
        scope: config.google.scope
     }); 
    res.redirect(url)
})

app.get('/details', (req, res, next) => {
    console.log(req.session.token.access_token);
    plus.people.get({
        userId: 'me',
        auth: oauthClient
    }, (err, userInfo) => {
        if(err){
            console.log("Error while fetching user info");
        }
        else{
            res.json(userInfo);   
        }
    });
});

app.listen(port, () => {
    console.log("Server listening at " + port)
});

0 个答案:

没有答案