【问题标题】:Editing Profile from HTML to MongoDB via Express / Node JS通过 Express / Node JS 将配置文件从 HTML 编辑到 MongoDB
【发布时间】:2017-11-15 15:08:03
【问题描述】:

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

【问题讨论】:

    标签: html node.js express mongoose mean-stack


    【解决方案1】:

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

    【讨论】:

    • 嗨 Alacritas,我已经尝试过了。我将 HTML 方法从“post”更改为“put”,将 router.post('/profile.html', profile.changeProfile) 更改为 router.put(XXX)。不幸的是,这没有奏效。我在一篇文章中读到 HTML 无法在表单中使用 put 方法。还是这样吗?
    猜你喜欢
    • 2022-11-21
    • 2022-08-18
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2019-01-24
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多