Mongodb用户架构错误

时间:2017-05-20 06:06:22

标签: mongodb express mean-stack

我正在尝试使用邮递员发送数据并创建我的用户。但我一直收到错误所以我注释掉了一些真正不需要的代码,现在我收到了这个错误。有谁知道我可能做错了什么?

Problem with mongo

Bottom of user code

Mongodb已连接且服务器正在端口3000上运行。

3 个答案:

答案 0 :(得分:1)

错误表明您将无效参数传递给parseFoo = try Parse AB <|> parse A ,即bcrypt.hash()而不是密码。检查是否定义了undefined

答案 1 :(得分:1)

在addUser函数中捕获newUser.password之前,请确保创建console.log(newUser)。在启动带有数据的新用户架构后,对象不会像您期望的那样到来。或者简单地给你自己的一个字符串做一个盐通,然后检查它是否正常工作。

答案 2 :(得分:1)

正如@Joe@robertklep已经提到的那样,您在第63行中遇到了错误:

  

bcrypt.hash(newUser.password,salt,(err,hash =&gt; {

因为变量密码未定义。 尝试手动定义它并确保代码,你所写的内容正确执行。

但我强烈建议您仔细查看at virtual field (in mongoose)并将其用于生成盐密码。

在您的情况下,您可以使用mongoose和bcrypt对此代码段执行相同的工作。

//首先,在模式中将“password”重命名为“clean_password”

user.virtual('clean_password')
    .set(function(clean_password) {
        this._password = clean_password;
        this.password = this.encryptPassword(clean_password);
    })
    .get(function() {
        return this._password;
    });

user.methods = {

    /**
     * Authenticate - check if the passwords are the same
     *
     * @param {String} plainText
     */
    authenticate: function(plainPassword) {
        return bcrypt.compareSync(plainPassword, this.password);
    },

    /**
     * Encrypt password
     *
     * @param {String} password
     */
    encryptPassword: function(password) {
        if (!password)
            return '';

        return bcrypt.hashSync(password, 10);
    }
};

并通过以下方式检查:

var user = {
    username: "whateveryouwant",
    clean_password: "whateveryouneed"
}

User.create(user, function(err,doc){});