如何在nodejs中嵌套json对象

发布于 2025-01-24 17:42:10 字数 4390 浏览 0 评论 0原文

我以MongoDB为数据库创建了一个NodeJS后端项目。我想创建一个文档,其中存储多个学生的详细信息。下面显示了我的控制器和模型文件。

Controller.js

const _user = new User({
  username: req.body.username,
  role: req.body.role,
  hash_password: hash_password,
  re_hash_password: req.body.re_hash_password,
  student:{
    member1: {
            fullName: req.body.student.member1.fullName,
            sliit_id: req.body.student.member1.sliit_id,
            phone: req.body.student.member1.phone,
            email: req.body.student.member1.email,
            specialization: req.body.student.member1.specialization,
    },
    member2: {
        fullName: req.body.student.member2.fullName,
        sliit_id: req.body.student.member2.sliit_id,
        phone: req.body.student.member2.phone,
        email: req.body.student.member2..email,
        specialization: req.body.student.member2.specialization,
    },  
  }

显示了我的Nodejs项目的控制器。在这里,我想将多个成员添加到我的MongoDB数据库中。

model.js

const demoStudentSchema = new mongoose.Schema({
    fullName: {
        type: String,
        required: true,
        trim: true,
        min: 3,
        max: 30
    },

    sliit_id: {
        type: String,
        required: true,
        trim: true,
        min: 3,
        max: 20
    },

    phone: {
        type: String,
        required: true
    },

    email: {
        type: String,
        required: true,
        trim: true,
        unique: true,
        lowercase: true
    },

    specialization: {
        type: String,
        enum: ['SE','CSNE','DS', 'ISE', 'CS']
    },

})
const userSchema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        trim: true,
        unique: true,
        index: true,
        min: 3,
        max: 20
    },

    role: {
        type: String,
        required:true,
        enum: ['student','supervisor','staff']
    },

    hash_password: {
        type: String,
        required: true,
    },

    re_hash_password: {
        type: String,
        required: true,
    },

    student: {
        member1: {
            demoStudentSchema
        },
        member2: {
            demoStudentSchema
        }
}
    
   

当我尝试使用Postman运行此代码时,会发生以下错误。

Postman请求和错误

{
    "message": {
        "errors": {
            "email": {
                "name": "ValidatorError",
                "message": "Path `email` is required.",
                "properties": {
                    "message": "Path `email` is required.",
                    "type": "required",
                    "path": "email"
                },
                "kind": "required",
                "path": "email"
            },
            "phone": {
                "name": "ValidatorError",
                "message": "Path `phone` is required.",
                "properties": {
                    "message": "Path `phone` is required.",
                    "type": "required",
                    "path": "phone"
                },
                "kind": "required",
                "path": "phone"
            },
            "sliit_id": {
                "name": "ValidatorError",
                "message": "Path `sliit_id` is required.",
                "properties": {
                    "message": "Path `sliit_id` is required.",
                    "type": "required",
                    "path": "sliit_id"
                },
                "kind": "required",
                "path": "sliit_id"
            },
            "fullName": {
                "name": "ValidatorError",
                "message": "Path `fullName` is required.",
                "properties": {
                    "message": "Path `fullName` is required.",
                    "type": "required",
                    "path": "fullName"
                },
                "kind": "required",
                "path": "fullName"
            }
        },
        "_message": "User validation failed",
        "name": "ValidationError",
        "message": "User validation failed: email: Path `email` is required., phone: Path `phone` is required., sliit_id: Path `sliit_id` is required., fullName: Path `fullName` is required."
    }
}

我已经浏览了一些视频和文章,但无法找到任何解决方案。 您能帮我找出错误,并就解决此问题提出建议。

I have created a nodejs backend project with mongodb as the database. I want to create a document where it stores details of multiple students. Below shows my controller and model files.

Controller.js

const _user = new User({
  username: req.body.username,
  role: req.body.role,
  hash_password: hash_password,
  re_hash_password: req.body.re_hash_password,
  student:{
    member1: {
            fullName: req.body.student.member1.fullName,
            sliit_id: req.body.student.member1.sliit_id,
            phone: req.body.student.member1.phone,
            email: req.body.student.member1.email,
            specialization: req.body.student.member1.specialization,
    },
    member2: {
        fullName: req.body.student.member2.fullName,
        sliit_id: req.body.student.member2.sliit_id,
        phone: req.body.student.member2.phone,
        email: req.body.student.member2..email,
        specialization: req.body.student.member2.specialization,
    },  
  }

Above shows the controller of my nodejs project. Here I want to add multiple members to my mongodb database with student object.

Model.js

const demoStudentSchema = new mongoose.Schema({
    fullName: {
        type: String,
        required: true,
        trim: true,
        min: 3,
        max: 30
    },

    sliit_id: {
        type: String,
        required: true,
        trim: true,
        min: 3,
        max: 20
    },

    phone: {
        type: String,
        required: true
    },

    email: {
        type: String,
        required: true,
        trim: true,
        unique: true,
        lowercase: true
    },

    specialization: {
        type: String,
        enum: ['SE','CSNE','DS', 'ISE', 'CS']
    },

})
const userSchema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        trim: true,
        unique: true,
        index: true,
        min: 3,
        max: 20
    },

    role: {
        type: String,
        required:true,
        enum: ['student','supervisor','staff']
    },

    hash_password: {
        type: String,
        required: true,
    },

    re_hash_password: {
        type: String,
        required: true,
    },

    student: {
        member1: {
            demoStudentSchema
        },
        member2: {
            demoStudentSchema
        }
}
    
   

When I try to run this code using postman, below error occurs.

Postman request and error

{
    "message": {
        "errors": {
            "email": {
                "name": "ValidatorError",
                "message": "Path `email` is required.",
                "properties": {
                    "message": "Path `email` is required.",
                    "type": "required",
                    "path": "email"
                },
                "kind": "required",
                "path": "email"
            },
            "phone": {
                "name": "ValidatorError",
                "message": "Path `phone` is required.",
                "properties": {
                    "message": "Path `phone` is required.",
                    "type": "required",
                    "path": "phone"
                },
                "kind": "required",
                "path": "phone"
            },
            "sliit_id": {
                "name": "ValidatorError",
                "message": "Path `sliit_id` is required.",
                "properties": {
                    "message": "Path `sliit_id` is required.",
                    "type": "required",
                    "path": "sliit_id"
                },
                "kind": "required",
                "path": "sliit_id"
            },
            "fullName": {
                "name": "ValidatorError",
                "message": "Path `fullName` is required.",
                "properties": {
                    "message": "Path `fullName` is required.",
                    "type": "required",
                    "path": "fullName"
                },
                "kind": "required",
                "path": "fullName"
            }
        },
        "_message": "User validation failed",
        "name": "ValidationError",
        "message": "User validation failed: email: Path `email` is required., phone: Path `phone` is required., sliit_id: Path `sliit_id` is required., fullName: Path `fullName` is required."
    }
}

I have gone through some videos and articles but couldnt find out any solution.
Could you please help me to find out the error and give your suggestions on solving this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文