首页 > 解决方案 > 我想在 mongodb 中创建一个包含数组字段的文档

问题描述

在 mongodb 架构中,我只想在此架构中添加一个数组,但在插入数据时遇到一些问题,始终返回错误字符串“此库(validator.js)仅验证字符串”

在模型中:

let prescriptionSchema = new Schema({

    appointment_id: {
        required: [true, 'Appointment id is required'],
        type :Schema.Types.ObjectId,
        ref: 'Appointments'
    },
    remarks: {
        type: String,
    },
    diagnosis_description:{ type : Array , "default" : [] },
    diagnosis: [{ 
        diagnosis_description: {
            type: String,
        }, 
    }],
    investigation: [{ 
        test_name: {
            type: String,
        }, 
        test_description: {
            type: String,
        }, 
    }],
    medicine: [{ 
        medicine_name: {
            type: String,
        }, 
        medicine_dosage: {
            type: String,
        }, 
        medicine_power: {
            type: String,
        }, 
    }]
}, 

{timestamps: {
        createdAt: 'created_at',
        updatedAt: 'updated_at'
}});

在控制器中我只是写了一个控制台日志,所以我的问题是在 mongodb 中,mongo 无法理解然后数组,请帮助.....

In postman :
{
    "appointment_id": "5d3ab1a4590ad324b2abdb76",
    "remarks" : "Next checkup after one month",
    "diagnosis":[
        {
            "diagnosis_description": "Fiver"
        }
    ],
    "investigation":[
        {
            "test_name" : "Blood",
            "test_description" : "Dengu"
        },
        {
            "test_name" : "Blood",
            "test_description" : "HIV"
        }
    ],
    "medicine":[
        {
            "medicine_name":"Paracetamol",
            "medicine_dosage":"650",
            "medicine_power":"AM"
        }
    ]
}

const path = require('path'), router = require('express').Router(), dir = ${path.dirname(__dirname)}/controllers, helperLib = require(path.resolve('./config/lib/helper_lib'));

这是路线:

let ReadDirectory   = new helperLib.read_directory.readDirectory();
let Middleware      = new helperLib.middleware();

//@ require all controllers for this module
let fileObj         = ReadDirectory.requireFiles(dir);


//@ routes mapping
router
    .put('/addprescription', fileObj['prescription.account'].addprescription);
    //.get('/prescriptionlist', fileObj['prescription.account'].prescriptionlist)
    //.put('/updateprescription', Middleware.decodeToken, fileObj['prescription.account'].updateprescription);


module.exports = {
    router: router,
    base: '/api/prescription'
};

标签: node.jsmongodbexpressmongoose

解决方案


请提供负责使用 validator.js 包的代码。发生的错误是说您可能正在尝试使用该库中的 validate 函数,该函数仅接受字符串参数。

该库仅验证和清理字符串。

链接:validator.js 包使用


推荐阅读