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



我正在尝试建立一个网络平台,您可以在其中注册和调整您的个人资料。但是,我在编辑部分苦苦挣扎。注册和登录很好,但其余的给出 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;

和配置文件控制器

/**
 * 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 用于添加新条目。

相关内容

  • 没有找到相关文章

最新更新