2017-02-12 41 views
0

我試圖做一個登記表,並保存到數據庫(通過MLAB雲DB),但提交表單後,服務器輪不到數據也沒有反應。我是MEAN Stack的新手,請告訴我是否犯了一些愚蠢的錯誤。 非常感謝提前。的Node.js/Express.js - 輪不到表單數據並保存到數據庫

controller.js

const bodyparser = require('body-parser'); 
const userModel = require('../model/userModel');app.use(bodyparser.urlencoded({extended:true})); 

//create acc 
app.post('/userSignUp', function(req, res){ 
    if (!req.body) return res.sendStatus(404); 

    var newUser = new userModel({ 
    username: req.body.username, 
    password: req.body.password 
    }); 

    //save user to db 
    newUser.save(function(err){ 
    if (err) throw err; 

    // fetch user and test password verification 
    userModel.findOne({username:req.body.username}, function(err, user){ 
    if (err) throw err; 

    // test a matching password 
    user.comparePassword(req.body.password, function(err, isMatch){ 
     if (err) throw err; 
     console.log(req.body.password, isMatch); 
     res.render('/index.ejs'); 
    }); 
    }); 
});}); 

註冊表單

<!-- Sign up form --> 
    <div id="signup_form" style="width:50%; margin:0 auto;"> 
     <h1>Sign up</h1> 
     <form action="userSignUp" method="get"> 
        <input type="text" name="username" placeholder="username"> 
        <input type="password" name="password" placeholder="password"> 
        <span> 
          <input type="checkbox" name="checkbox"> 
          <label for="checkbox">remember</label> 
        </span> 
        <input type="submit" value="Sign Up"> 
      </form> 
    </div> 

的usermodel

var mongoose = require('mongoose'), 
Schema = mongoose.Schema, 
bcrypt = require('bcrypt'), 
SALT_WORK_FACTOR = 10; 

var UserSchema = new Schema({ 
username: { type: String, required: true, index: { unique: true } }, 
password: { type: String, required: true } 

});

UserSchema.pre('save', function(next) { 
var user = this; 

// only hash the password if it has been modified (or is new) 
if (!user.isModified('password')) return next(); 

// generate a salt 
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { 
if (err) return next(err); 

// hash the password using our new salt 
bcrypt.hash(user.password, salt, function(err, hash) { 
    if (err) return next(err); 

    // override the cleartext password with the hashed one 
    user.password = hash; 
    next(); 
}); 

});

});

UserSchema.methods.comparePassword = function(candidatePassword, cb) { 
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { 
    if (err) return cb(err); 
    cb(null, isMatch); 
}); 

};

module.exports = mongoose.model('User', UserSchema); 

Developer Tools

+0

你可以添加你的'../model/userModel。 js'文件? – Benni

+0

它會拋出任何錯誤嗎? – Benni

+0

順便說一句,你不應該發回'404'(意思是「未找到」),但'400'(「錯誤的請求」) – Benni

回答

0

您使用GET方法的方法,而在快遞路由器使用的是app.post

使用方法= 「後」,這樣就可以從body.property

得到
<form action="userSignUp" method="post"> 
+0

更正爲「後」,但仍然沒有工作! – SoilChau

+0

在Chrome網絡請求選項卡單擊請求URL並查看預覽響應代碼,狀態代碼或錯誤。一定有東西。 atlease要發送到服務器什麼,什麼服務器發回 – owaishanif786

+0

我也請找上面的開發工具。經過漫長的等待,頁面導致錯誤「ERR_EMPTY_RESPONSE」 – SoilChau