Node JS cannot post error?

佐手、 提交于 2019-12-12 02:33:16

问题


I am unsure as to why I am getting an error for POST-ing a form. I am using mongoose to connect to mongodb and using Jade as my view engine. I am trying to POST an update and not a new account into the db, and using a cookie to pull the user's info. So it is the Edit User Profile page. Everything works on the Jade file and looks right, just when I hit the submit button it goes to a:

Cannot POST /editUserProfile

My mongoose User file:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    bcrypt = require('bcrypt-nodejs'),
    SALT_WORK_FACTOR = 10;


var UserSchema = new Schema({ 
    email: { type: String, required: true, lowercase:true, index: { unique: true } },
    password: { type: String, required: true },
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    phone: {type: Number, required: true},
    birthday: {type: Date, required: true}
});


UserSchema.pre('save', function(next) {
    var user = this;
    console.log("email exists");
    // only hash the password if it has been modified (or is new)
    if (!user.isModified('password')) return next();
    // generate a salt
    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
        if (err) return next(err);
        // hash the password along with our new salt
        bcrypt.hash(user.password, salt, null, function(err, hash) {
            if (err) return next(err);
            // override the cleartext password with the hashed one
            user.password = hash;
            next();
        });
    });    
});

UserSchema.methods.comparePassword = function(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    });
};


module.exports = mongoose.model('User', UserSchema);

My route file:

exports.editUserProfile = function(req, res) {
    User.findById(req.signedCookies.userid, function(err,user) {
        if(err) {
            res.send(err);
        } else {
            console.log(JSON.stringify(user));
    res.render('editUserProfile', {title: 'Weblio', 
        ufirstName: user.firstName,
        ulastName: user.lastName,
        uemail: user.email,
        uphone: user.phone,
        ubirthday: user.birthday
    });
    //, user: user.firstName, - taken out after title
        }
    });
};

exports.editUserProfilePost = function(req, res) {
        var updateUser = new User ({
            firstName: req.body.firstName,
            lastName: req.body.lastName,
            email: req.body.email,
            phone: req.body.phone,
            birthday: new Date(req.body.birthday) 
        });
        updateUser.save(function(err){
            console.log("here 3");
            if(!err) {
                console.log("here3a");
                res.render('userProfile', {title: 'Weblio'}); 

            } else {
                console.log("here 1a");
                (new Error('Incorrect POST'));
                return res.render('editUserProfileError', {title: 'Weblio'}); 
            }
        });

};

Jade file:

extends layout
block content   
    div.centerContent

        form.validateForm(method="POST", action="/editUserProfile")
                legend Edit Profile
                input(type="text", name="firstName", maxlength="20", placeholder=ufirstName, value=ufirstName)
                br
                input(type="text", name="lastName", maxlength="20", placeholder=ulastName, value=ulastName)
                br
                input(type="email", name="email", maxlength="20", placeholder=uemail, value=uemail)
                br
                input(type="number", name="phone", maxlength="20", placeholder=uphone, value=uphone)
                br
                input(type="date", name="birthday", placeholder=ubirthday, value=ubirthday)
                br
                input.btn.btn-primary(type="submit", name="Update", value="Save")
                a(href="/userProfile")
                    button.btn(type="button") Cancel

This is my app.js file: I have a bunch of other things in there but the Register part is getting POST-ed so I don't think the app.js has any problems.

  app.get('/editUserProfile', user.editUserProfile);
  app.post('/editUserProfile', user.editUserProfilePost);

Updated:

exports.editUserProfilePost = function(req, res, err) {


            User.findByIdAndUpdate(req.signedCookies.userid,{
            firstName: req.body.firstName,
            lastName: req.body.lastName,
            email: req.body.email,
            phone: req.body.phone,
            birthday: new Date(req.body.birthday) 
        }, function(err) {
            if(!err) {
            console.log("post2");
            res.render('userProfile', {title: 'Weblio'}); 

        } else {
            console.log("post3");
            (new Error('Incorrect POST'));
            return res.render('editUserProfileError', {title: 'Weblio'}); 
        }
        });

};

来源:https://stackoverflow.com/questions/17633049/node-js-cannot-post-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!