Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点JS无法发布错误?_Javascript_Node.js_Pug - Fatal编程技术网

Javascript 节点JS无法发布错误?

Javascript 节点JS无法发布错误?,javascript,node.js,pug,Javascript,Node.js,Pug,我不确定为什么我在发布表单时出错。我使用mongoose连接到mongodb,并使用Jade作为我的视图引擎。我试图发布一个更新,而不是一个新的帐户到数据库,并使用cookie拉用户的信息。因此,它是“编辑用户配置文件”页面。Jade文件上的所有内容都正常工作,并且看起来不错,当我点击提交按钮时,它会显示: Cannot POST /editUserProfile 我的mongoose用户文件: var mongoose = require('mongoose'), Schema =

我不确定为什么我在发布表单时出错。我使用mongoose连接到mongodb,并使用Jade作为我的视图引擎。我试图发布一个更新,而不是一个新的帐户到数据库,并使用cookie拉用户的信息。因此,它是“编辑用户配置文件”页面。Jade文件上的所有内容都正常工作,并且看起来不错,当我点击提交按钮时,它会显示:

Cannot POST /editUserProfile
我的mongoose用户文件:

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);
我的路线文件:

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'}); 
            }
        });

};
翡翠档案:

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
这是我的app.js文件:我还有很多其他的东西在里面,但是注册部分已经发布了,所以我认为app.js没有任何问题

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

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'}); 
        }
        });

};

您确实有一个
app.post('/editUserProfile',…)
路由到某个地方,对吗?是的,让我更新代码。
editUserProfilePost中的控制台消息也不会显示?错误消息显示404错误,这很奇怪。你没有使用任何可能导致这种反应的中间件?我不知道发生了什么。我使用完全相同的堆栈,并且我以类似的方式编写代码,在我看来一切正常。唯一不同的是,我认为这与您的问题无关:我将使用
module.exports.editUserProfilePost
,而不是
exports.editUserProfilePost
!你能试试吗?是的,它不会改变任何事情。我没有使用任何中间件,我能想到的唯一一件事是,在mongoose模式中是否有我做得不对的事情?我假设save插入/更新了用户,所以它应该是相同的,还是我想在mongoose中添加一些东西来更新用户的信息?