2017-06-02 18 views
1

Expressでユーザーを作成しようとしています。Express&Mongooseで新しいユーザーを保存できません

localhost:3000/users/register?first_name=1&last_name=1&email=1&password=123456&country=1&city=1&street=1&number=1 

そして、私は、コンソール上で、このエラーを取得しています: は、私は、次のURLスルーポストマンを経てのparam URLで新しいユーザ・データのすべてにPOSTリクエストをしています

There was an erorrError: Illegal arguments: undefined, string

で私はstudent.jsを作成したモデルフォルダです、それはユーザです。ルートフォルダのユーザーのルータで

const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 
const bcrypt = require('bcryptjs'); 


const StudentSchema = new Schema({ 
    first_name: String, 
    last_name: String, 
    email:{ 
     type: String, 
     required: true, 
     unique: true 
    }, 
    password: { 
     type: String, 
     required: true 
    }, 
    address: 
     { country: String, 
      city: String, 
      street: String, 
      number: Number 
     }, 
    created_at: Date, 
    updated_at: Date 
}); 
StudentSchema.pre('save', function(next) { 
    var currentDate = new Date(); 
    this.updated_at = currentDate; 
    if (!this.created_at) 
     this.created_at = currentDate; 

    next(); 
}); 
var Student = mongoose.model('Student', StudentSchema); 
module.exports = Student; 

module.exports.addStudent = function(newStudent, callback){ 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(newStudent.password, salt, function(err, hash) { 
      if(err) { 
       console.log(hash); 
       **console.log("There was an erorr" + err);** 
      }else { 
       newStudent.password = hash; 
       newStudent.save(callback); 
      } 
     }); 
    }); 
}; 

var express = require('express'); 
var router = express.Router(); 
var Student = require('../models/student'); 
var mongodb = require('mongodb'); 

router.post('/register', function(req, res, next) { 
    var newStudent =new Student({ 
     first_name: req.body.first_name, 
     last_name: req.body.last_name, 
     email: req.body.email, 
     password: req.body.password, 
     address: 
      { 
       country: req.body.country, 
       city: req.body.city, 
       street: req.body.street, 
       number: req.body.number 
      } 
    }); 

    Student.addStudent(newStudent, function(err,user) { 
    if(err){ 
     res.json({success: false, msg:'Failed to register user'}); 
    } else { 
     res.json({success: true, msg:'User registered'}); 
    } 
    }); 
}); 

router.get('/newstudent', function(req, res) { 
    res.render('newstudent', { title: 'Add student' }); 
}); 

module.exports = router; 

は、私が "**"

あなたは クエリパラメータとポストマンにデータを送信している

答えて

0

(とコード行ブレーキをmarkeredましたURLに表示されます)、req.bodyにデータを抽出すると予想されます。

郵便受けの送信リクエストの本文や明示的なクエリのパラメータのいずれかを変更することはできますが、表示されにくく安全性が低いため、URLのような機密データをURLに送信することはおすすめしません。

ですから、あなたがこのように身体にポストマンにデータを送信する方法を変更する必要があります

enter image description here

+0

働く、THANKS !!!! –

+0

@ModiNavon喜んで私は助けてくれた、あなたがそれを解決したと思うなら、私の答えを受け入れてください。 –

関連する問題