Node.js“无法设置未定义的属性firstName”

时间:2017-12-04 10:07:04

标签: mysql node.js express

我正在学习本教程,

https://www.youtube.com/watch?v=OnuC3VtEQks

为我的节点应用程序创建一个登录系统,唯一的事情就是因为他正在使用mongoDb而我正在使用mySql,我不得不想办法绕过他设置他的猫鼬模式(约7:14左右)视频),所以我只是在创建用户逻辑时导出了一个用户模块并为其分配了属性。

这是用户模块

//user.js in models
var bcrypt = require('bcryptjs');

var User = {
    firstName: "firstName",
    lastName: "lastName",
    email: "email",
    username: "username",
    password: "password",
}

module.exports = User;

module.exports.createUser = function(newUser, callback){
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(newUser.password, salt, function(err, hash) {
            newUser.password = hash;
            newUser.save(callback);
        });
    });
}

以下是我的注册表格路线

// main.js in router folder
var user = require('../models/user').User;

router.post('/register', function(req, res){
    var firstName = req.body.firstName;
    var lastName = req.body.lastName;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var cpassword = req.body.cpassword;

    //Validation
    // req.checkBody('firstName', 'First name is required!').notEmpty();
    // req.checkBody('lastName', 'Last name is required!').notEmpty();
    // req.checkBody('email', 'E-mail is invalid!').isEmail();
    // req.checkBody('username', 'Username is required!').notEmpty();
    // req.checkBody('password', 'Password is required!').notEmpty();
    // req.checkBody('cpassword', 'Passwords do not match!').equals(password);

    var errors = req.validationErrors();
    if(errors){
        res.render('register', {
            errors: errors
        });
        console.log(errors);
    } else {
        var newUser = user;

        newUser.firstName = firstName;
        newUser.lastName = lastName;
        newUser.email = email;
        newUser.username = username;
        newUser.password = password;

        User.createUser(newUser, function(err, user){
            if(err) throw err;
            console.log(user);
        });

        req.flash('success_msg', 'You are now registered!');
        res.redirect('login');
    }
});

现在我不断收到“无法设置未定义错误的属性firstName”,但我认为我可以从user.js模块访问我的User对象?从我可以告诉它看起来他只是使用表单输入来实例化用户对象并使用一些密码哈希创建它。

如果这是一个糟糕的方法,我完全愿意接受任何想法,使这更简单。我对节点完全陌生,我的高级项目将在两周后到期,因此任何帮助都将得到极大的赞赏,并获得赞成和互联网积分:)

2 个答案:

答案 0 :(得分:1)

您应该按如下方式更改您的user.js:

//user.js in models
var bcrypt = require('bcryptjs');

var User = {
    firstName: "firstName",
    lastName: "lastName",
    email: "email",
    username: "username",
    password: "password",
}

module.exports = {
    User: User,
    createUser: function (newUser, callback) {
        bcrypt.genSalt(10, function (err, salt) {
            bcrypt.hash(newUser.password, salt, function (err, hash) {
                newUser.password = hash;
                newUser.save(callback);
            });
        });
    }
}

这可以解决你所有的问题。

答案 1 :(得分:0)

您在程序的前面声明(并导出)对象User(带有大写U),但稍后尝试访问变量user,这是未定义的

var newUser = user;