2017-06-13 59 views
2

我试图构建一个web平台,您可以在其中注册和修改您的配置文件。但是,我正在努力编辑部分。注册和登录都很好,但其余部分给出了一个HTTP 500 因此,这里是我所做的:通过Express/Node编辑从HTML到MongoDB的配置文件JS

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内部服务器错误。

回答

0

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

+0

嗨Alacritas,我已经试过了。我将HTML方法从“post”更改为“put”,将router.post('/ profile.html',profile.changeProfile)更改为'router.put(XXX)'。这不起作用。我在一篇文章中读到HTML无法在表单中使用put方法。那仍然是这样吗? – dominikger