通过Express/Node JS将配置文件从HTML编辑到MongoDB

通过Express/Node JS将配置文件从HTML编辑到MongoDB,html,node.js,express,mongoose,mean-stack,Html,Node.js,Express,Mongoose,Mean Stack,我正在尝试建立一个网络平台,在那里你可以注册和修改你的个人资料。然而,我正在为编辑部分而挣扎。注册和登录都可以,但其余部分提供HTTP 500。 以下是我所做的: User Scheme for Mongoose: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var passportLocalMongoose = require('passport-local-mongoose'); //enhance

我正在尝试建立一个网络平台,在那里你可以注册和修改你的个人资料。然而,我正在为编辑部分而挣扎。注册和登录都可以,但其余部分提供HTTP 500。 以下是我所做的:

User Scheme for Mongoose: 
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');


//enhanced userSchema
var UserSchema = new Schema({
    user_id: Schema.ObjectId,
    username: {type :String, required : true, unique : true}, //serves as unique identifier
    password: {type : String, required: true},
    name: {type : String, required : true},
    surname: String,
    created_at : Date,
    updated_at : Date,
    skills: [{type : String}],
    lectures: [{type : String}],
    groups: [{type : String}] //todo: Change for later cross referencing with group schemes
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', UserSchema);
然后是路由:

var express = require('express');
var router = express.Router();
var auth = require("../controllers/AuthController.js");
var profile = require ("../controllers/ProfileController");

// restrict index for logged in user only
router.get('/', auth.home);

// route to register page
router.get('/register.html', auth.register);

// route for register action
router.post('/register.html', auth.doRegister);

// route to login page
router.get('/login.html', auth.login);

// route for login action
router.post('/login.html', auth.doLogin);

// route for logout action
router.get('/logout.html', auth.logout);

//route to profile
router.get('/profile.html', profile.goToProfile);

//route for changing profile
router.post('/profile.html', profile.changeProfile);


module.exports = router;
和profileController

/**
 * Controller for editing the profile
 */

var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var path = require('path');

//Change Name

var profileController = {};

//go to Profile
profileController.goToProfile = function (req, res){
    res.sendFile(path.resolve('login.html')), {user : req.user};
}

profileController.changeProfile= function (req, res){
    console.log("REQUEST: " + req.body.toString());
    if (req.body.surname.isEmpty()){

    }
    else {
        User.findByIdAndUpdate(req.user._id, { $set: { surname: req.body.surname }}, { new: true }, function (err, User) {
            if (err) {
                console.log(err.toString());}
            res.alert('Changed surname');
            console.log('changed surname')
        });
    };
    if (req.body.name.isEmpty()){}
    else {
        User.findByIdAndUpdate(req.user._id, { $set: { name: req.body.name }}, { new: true }, function (err, User) {
            if (err) {
                console.log(err.toString());}
            res.alert('Changed name');
            console.log('changed name')
        });

    };

    if (req.body.skills.length === 0){}
    else {
        User.findByIdAndUpdate(req.user._id, { $set: { skills: req.body.skills }}, { new: true }, function (err, User) {
            console.log("Old Skills: " + User.skills.toString());
            if (err) {
                console.log(err.toString());}
            console.log("New skills: " + User.skills.toString());
            console.log('changed skills')
        });

    }
};

module.exports = profileController;
从以下HTML表单获取其数据:

<!-- register container -->
<div class="container">
    <form role="form" action="profile.html" method="post" style="max-width: 300px;">
        <h2 class="form-heading">Your Profile</h2>
        <input type="text" name="name" placeholder="Name" class="form-control" />
        <input type="text" name="username" placeholder="Username" class="form-control" />
        <input type="text" name="surname" placeholder="Last Name" class="form-control"/>
        <input type="text" name="skills[]" placeholder="Your skills" class="form-control"/>
        <button type="submit" class="btn btn-lg btn-primary btn-block">Save</button>
    </form>
</div>

你的个人资料
拯救
我为我糟糕的代码质量感到非常抱歉。这是由于我花了很长一天的时间来研究它,但我根本不知道(即使有教程和堆栈溢出)哪里出了问题


结果是500内部服务器错误

您的PUT请求在哪里?要更新数据,需要使用PUT请求。POST用于添加新条目

嗨,阿拉克里塔斯,我已经试过了。我将HTML方法从“post”更改为“put”,并将
router.post('/profile.HTML',profile.changeProfile)
更改为
router.put(XXX)
。不幸的是,这不起作用。我在一篇文章中读到HTML不能在表单中使用put方法。还是这样吗?