node.js - 我想在 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'
};
解决方案
请提供负责使用 validator.js 包的代码。发生的错误是说您可能正在尝试使用该库中的 validate 函数,该函数仅接受字符串参数。
该库仅验证和清理字符串。
推荐阅读
- python - Pandas - 使用一系列值过滤行
- html - 为什么在源上调整大小时图像会丢失质量,但在使用 html 调整大小时却不会
- javascript - Javascript - 遍历一系列约会,寻找下一个可用的
- c++ - c++ 代码的行为取决于编译器
- ios - 准备 segue 时致命错误索引超出范围
- continuous-integration - 在 Github 操作中获取分支提交
- node.js - NodeJS 使用 pdf 创建器通过电子邮件发送 PDF
- compression - deflate 是否可以很好地压缩紧密交错的可压缩和不可压缩数据?
- oracle - JBoss 6.4 - Oracle 数据库连接丢失
- javascript - 在 React 中将 onChange 事件从子级传递给父级