Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t使用Express&;存储新用户;猫鼬_Javascript_Node.js_Express - Fatal编程技术网

Javascript Can';t使用Express&;存储新用户;猫鼬

Javascript Can';t使用Express&;存储新用户;猫鼬,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试使用express创建一个用户。 我正在通过邮递员通过以下URL在param URL上对所有新用户数据进行POST请求: localhost:3000/users/register?first_name=1&last_name=1&email=1&password=123456&country=1&city=1&street=1&number=1 我在控制台上看到这个错误: 出现错误:非法参数:未定义,字符串 在我创建的stud

我正在尝试使用express创建一个用户。 我正在通过邮递员通过以下URL在param URL上对所有新用户数据进行POST请求:

localhost:3000/users/register?first_name=1&last_name=1&email=1&password=123456&country=1&city=1&street=1&number=1
我在控制台上看到这个错误:

出现错误:非法参数:未定义,字符串

在我创建的student.js的model文件夹中,它是一个用户

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;

我将代码行标记为“**”

您正在邮递员中发送带有query参数(显示在url中)的数据,并希望提取
req.body
中的数据

您可以更改其中一个(在postman send请求正文中,或在express extract查询参数中),但不建议在URL中以用户名/密码等格式发送敏感数据,因为它是可见的且不太安全

因此,您应该改变邮递员向身体发送数据的方式,如下所示:


您正在使用查询参数(显示在url中)在邮递员中发送数据,并希望在
请求正文中提取数据

您可以更改其中一个(在postman send请求正文中,或在express extract查询参数中),但不建议在URL中以用户名/密码等格式发送敏感数据,因为它是可见的且不太安全

因此,您应该改变邮递员向身体发送数据的方式,如下所示:


@ModiNavon很高兴我帮了忙,如果你认为我的答案解决了问题,请接受我的答案。@ModiNavon很高兴我帮了忙,如果你认为我的答案解决了问题,请接受我的答案。