繁体   English   中英

在Express JS中访问用户信息返回Undefined

[英]Accessing user information in Express JS is returning Undefined

我目前正在使用Passport在我的应用程序中进行身份验证。 当尝试提取用户电子邮件以将其与其他信息一起存储在数据库中时,我得到的返回值是undefined。 如果拉出整个用户对象,我将获得正确的信息。 见下文。

这是初始化会话的server.js文件。

app.use(session({
    secret: 'sessionSecret'
})); 
app.use(passport.initialize());
app.use(passport.session()); 
app.use(flash());

这是路线信息

app.get('/itemCreation', function (req, res) {
    res.render('itemCreation.ejs', {
        user: req.user 
    });
});
app.post('/itemCreation', function (req, res) {
    var item = new itemSchema();
    item.local.productName = req.body.productName;
    item.local.itemPrice = req.body.itemPrice;
    item.local.Quantity = req.body.Quantity;
    item.local.Description = req.body.Description;
    console.log(req.user.firstName);
    item.save(function (err) {
        if (err)
            throw err;
        else
            console.log('Saved item information successfully');

    });
    res.redirect('/shop');
});

这是我的物品模型

var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');

var itemSchema = mongoose.Schema({

    local            : {
        productName     : String,
        itemPrice       : Number,
        Quantity        : Number,
        Description     : String
        }

});

module.exports = mongoose.model('item', itemSchema);

这是拉动整个对象的结果 ,我可以通过调用

console.log(req.user);

这是从对象中仅提取电子邮件的结果 ,我可以通过调用

console.log(req.user.email);

应该是console.log(req.user.local.email);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM