javascript - Error: TypeError: Cannot read property 'catch' of undefined when trying to register user for websit

I seem to have messed up one of my promises (I think) in a javascript function that is supposed to regi

I seem to have messed up one of my promises (I think) in a javascript function that is supposed to register a user. I have included the post request and the actual function itself.

app.post("/register", (req, res) => {
    dataServiceAuth.registerUser(req.body).then(() => {
        res.render("register", {successMessage: "User created"});
    }).catch((err) => {
        res.render("register", {errorMessage: err, user: req.body.user});
    });
});



module.exports.registerUser = function (userData) {
    return new Promise(function (resolve, reject) {
        if (userData.password != userData.password2) {
            reject("Passwords do not match");
        }
        else {
            let newUser = new User(userData);

            newUser.save((err) => {
                resolve();
            }).catch((err) => {
                if (err) {
                    if (err.code == 11000) {
                        reject('User Name already taken');
                    }
                    else {
                        reject('There was an error creating the user: ${err}');
                    }
                }
            });
        }
    });
};

I seem to have messed up one of my promises (I think) in a javascript function that is supposed to register a user. I have included the post request and the actual function itself.

app.post("/register", (req, res) => {
    dataServiceAuth.registerUser(req.body).then(() => {
        res.render("register", {successMessage: "User created"});
    }).catch((err) => {
        res.render("register", {errorMessage: err, user: req.body.user});
    });
});



module.exports.registerUser = function (userData) {
    return new Promise(function (resolve, reject) {
        if (userData.password != userData.password2) {
            reject("Passwords do not match");
        }
        else {
            let newUser = new User(userData);

            newUser.save((err) => {
                resolve();
            }).catch((err) => {
                if (err) {
                    if (err.code == 11000) {
                        reject('User Name already taken');
                    }
                    else {
                        reject('There was an error creating the user: ${err}');
                    }
                }
            });
        }
    });
};
Share Improve this question asked Apr 13, 2018 at 3:09 Jasper FrenchJasper French 311 gold badge1 silver badge6 bronze badges 2
  • 3 Seems like newUser.save is not returning something catchable, but newUser.save was not posted in your code. – CertainPerformance Commented Apr 13, 2018 at 3:11
  • Check if newUser.save is a Node style callback which is passed a falsey value for the err parameter when the operation succeeded, or a non-falsey value (includes non zero numbers) if an error occurred. – traktor Commented Apr 13, 2018 at 3:39
Add a ment  | 

1 Answer 1

Reset to default 3

If newUser.save can return a promise, you definitely shouldn’t be passing a callback to it, or even using the Promise constructor at all. If you really want to reject with strings, the way to implement that would be by transforming rejections from newUser.save() with .catch into new rejections by returning them, and returning the resulting promise from registerUser:

module.exports.registerUser = function (userData) {
    if (userData.password != userData.password2) {
        return Promise.reject("Passwords do not match");
    }

    let newUser = new User(userData);

    return newUser.save().catch((err) => {
        if (err.code == 11000) {
            return Promise.reject('User Name already taken');
        }
        else {
            return Promise.reject('There was an error creating the user: ${err}');
        }
    });
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742410810a4438774.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信