Node.js 节点快速注册功能不工作

Node.js 节点快速注册功能不工作,node.js,express,promise,async-await,Node.js,Express,Promise,Async Await,此函数是一个注册函数,它: 1) 检查表单中输入的电子邮件是否已存在于数据库中 2) 如果电子邮件不存在,则哈希密码 3) 创建用户实例并将用户保存到mongo集合 4) 向注册的电子邮件发送验证令牌以验证帐户 我在(3)中遇到一个错误,在尝试将用户保存到数据库后调用.catch,即承诺无法解决。我试着在constnewuser=newUser({…})之后取消这个承诺,到那时为止一切都正常运行。因此,我将错误范围缩小到:newUser无法保存到集合中。我怀疑我错误地通过async.瀑布()中的

此函数是一个注册函数,它: 1) 检查表单中输入的电子邮件是否已存在于数据库中 2) 如果电子邮件不存在,则哈希密码 3) 创建用户实例并将用户保存到mongo集合 4) 向注册的电子邮件发送验证令牌以验证帐户

我在(3)中遇到一个错误,在尝试将用户保存到数据库后调用.catch,即承诺无法解决。我试着在
constnewuser=newUser({…})
之后取消这个承诺,到那时为止一切都正常运行。因此,我将错误范围缩小到:
newUser
无法保存到集合中。我怀疑我错误地通过
async.瀑布()
中的每个函数传递了参数,但我希望能够深入了解潜在的问题

auth.js

const async = require("async");
const bcrypt = require("bcryptjs");
const nodemailer = require("nodemailer");

const User = require("..models/User").Model;

controller.register = (req, res) => {
    // grab user data from registration form body
    const { fname, lname, email, password } = req.body;

    const validateEmail = (callback) => {
        User.findOne({ "email.address": email})
        .then(user => {
            if(user) {
                return res.status(403).json({
                    error: "This is email is already associated with a registered account"
                });
            } else {
                callback(null, fname, lname, email, password);
            };
        })
        .catch(err => {
            return res.status(500).json({
                errors: err.message || "The server experienced an error while validating this email"
            });
        });
    };

    // hash password from the form and pass form data to the database collection
    const hashPassword = (fname, lname, email, password, callback) => {
        bcrypt.genSalt(10, (err, salt) => {
            bcrypt.hash("", salt, (err, hash) => {
                if(err) {
                    return res.status(500).json({
                        message: "The server was unable to hash your password",
                        errors: err
                    });
                } else {
                    password = hash;

                    callback(null, fname, lname, email, hash);
                };
            });
        });
    };

    const registerUser = (fname, lname, email, hash, callback) => {
        // create user instance from form data and hashed password
        const newUser = new User({    
            "name.first": fname,
            "name.last": lname,
            "email.address": email,
            password: hash
        });

        // save user to database collection and pass user info to next function
        newUser.save()
        .then(user => {
            callback(null, user);
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                message: err.messsage || "An error occured while registering your new account"
            });
        });
    };

    const sendVerificationEmail = (user, callback) => {
        // use nodemailer to send an verification token to the registered email address to verify the new account
    };

    // run series of functions above
    async.waterfall([
        validateEmail, // check whether registered email is already in database
        hashPassword, // if not, hash the password
        registerUser, // save the new user to the database collection
        sendVerificationEmail // send verification token to email to verify the account
    ], (err, results) => {
        if (err) {
            return res.status(500).json({
                message: "The server was unable to complete your request",
                errors: err
            });
        } else {
            return res.status(201).json(results);
        }
    });
}
const Schema = require("mongoose").Schema;
const model = require("mongoose").model;

const UserSchema = new Schema({
    _id: Schema.Types.ObjectId,
    name: {
        first: {type: String, required: true},
        last: {type: String, required: true}
    },
    email: {
        address: {type: String, required: true, unique: true},
        verified: {type: Boolean, default: false}
    },
    password: {type: String, required: true, minlength: 6}, 
});

module.exports = {
    Schema: UserSchema,
    Model: model("User", UserSchema)
}
以下是我的用户模型供参考:
User.js

const async = require("async");
const bcrypt = require("bcryptjs");
const nodemailer = require("nodemailer");

const User = require("..models/User").Model;

controller.register = (req, res) => {
    // grab user data from registration form body
    const { fname, lname, email, password } = req.body;

    const validateEmail = (callback) => {
        User.findOne({ "email.address": email})
        .then(user => {
            if(user) {
                return res.status(403).json({
                    error: "This is email is already associated with a registered account"
                });
            } else {
                callback(null, fname, lname, email, password);
            };
        })
        .catch(err => {
            return res.status(500).json({
                errors: err.message || "The server experienced an error while validating this email"
            });
        });
    };

    // hash password from the form and pass form data to the database collection
    const hashPassword = (fname, lname, email, password, callback) => {
        bcrypt.genSalt(10, (err, salt) => {
            bcrypt.hash("", salt, (err, hash) => {
                if(err) {
                    return res.status(500).json({
                        message: "The server was unable to hash your password",
                        errors: err
                    });
                } else {
                    password = hash;

                    callback(null, fname, lname, email, hash);
                };
            });
        });
    };

    const registerUser = (fname, lname, email, hash, callback) => {
        // create user instance from form data and hashed password
        const newUser = new User({    
            "name.first": fname,
            "name.last": lname,
            "email.address": email,
            password: hash
        });

        // save user to database collection and pass user info to next function
        newUser.save()
        .then(user => {
            callback(null, user);
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                message: err.messsage || "An error occured while registering your new account"
            });
        });
    };

    const sendVerificationEmail = (user, callback) => {
        // use nodemailer to send an verification token to the registered email address to verify the new account
    };

    // run series of functions above
    async.waterfall([
        validateEmail, // check whether registered email is already in database
        hashPassword, // if not, hash the password
        registerUser, // save the new user to the database collection
        sendVerificationEmail // send verification token to email to verify the account
    ], (err, results) => {
        if (err) {
            return res.status(500).json({
                message: "The server was unable to complete your request",
                errors: err
            });
        } else {
            return res.status(201).json(results);
        }
    });
}
const Schema = require("mongoose").Schema;
const model = require("mongoose").model;

const UserSchema = new Schema({
    _id: Schema.Types.ObjectId,
    name: {
        first: {type: String, required: true},
        last: {type: String, required: true}
    },
    email: {
        address: {type: String, required: true, unique: true},
        verified: {type: Boolean, default: false}
    },
    password: {type: String, required: true, minlength: 6}, 
});

module.exports = {
    Schema: UserSchema,
    Model: model("User", UserSchema)
}

你能为用户模型添加代码吗?@SuleymanSah完成,请再次查看!你能试试这个吗?const newUser=新用户({name:{first:fName,last:lname},email:{address:email},password:hash});那绝对是个错误!我刚刚修复了newUser,但是我仍然收到相同的错误。这里的错误日志是什么。在newUser.save()中捕获(err=>{console.log(err)…)