gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:40:58 25 4
gpt4 key购买 nike

我正在尝试构建一个网络平台,您可以在其中注册和调整您的个人资料。但是,我正在为编辑部分而苦苦挣扎。 Registration 和 Login 没问题,但其余的给出了 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

/**
* 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 - 通过 Express/Node JS 将配置文件从 HTML 编辑到 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44528250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com