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;
は、私が "**"
あなたは クエリパラメータとポストマンにデータを送信している
働く、THANKS !!!! –
@ModiNavon喜んで私は助けてくれた、あなたがそれを解決したと思うなら、私の答えを受け入れてください。 –