我一直试图在nodejs中插入一个数组,但是我一直无法做到这一点,错误不断出现在路径上
我想插入一个要存储在mongo db地图集上的数组,但是我不断收到错误消息,直到我将IDSchema引入代码后,该错误才不存在
/*model file*/
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
require('mongoose-currency').loadType(mongoose);
var IDschema = new Schema({
vanId: {type: String, required: true},
driverId: {type: String, required: true}
})
var driverschema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
name: {type: String, required: true},
age: {type: String, required: true},
phonenumber: {type: String, required: true},
address: {type: String, required: true},
dob: {type: String, required: true},
designation: {type: String, required: true},
ID:[IDschema]
});
var Driver = mongoose.model('Driver', driverschema);
module.exports = Driver
/*routes file*/
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Driver = require('../model/driverdetails');
router.post('/driverdetails', function(req, res){
const driverschema = new Driver({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
age: req.body.age,
phonenumber: req.body.phonenumber,
address: req.body.address,
dob: req.body.dob,
designation: req.body.designation,
ID: req.body.ID
});
driverschema.save()
.then(result =>{
console.log(result);
res.status(200).json(result);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
})
});
module.exports = router;
这是我得到的错误
{名称: {ValidatorError:路径
name
是必需的。 在新的ValidatorError(C:\ projects \ JWT \ node_modules \ mongoose \ lib \ error \ validator.js:29:11) 在验证时(C:\ projects \ JWT \ node_modules \ mongoose \ lib \ schematype.js:930:13) 在C:\ projects \ JWT \ node_modules \ mongoose \ lib \ schematype.js:983:11 在Array.forEach() 在SchemaString.SchemaType.doValidate(C:\ projects \ JWT \ node_modules \ mongoose \ lib \ schematype.js:939:19) 在C:\ projects \ JWT \ node_modules \ mongoose \ lib \ document.js:1936:9 在_combinedTickCallback(内部/进程/next_tick.js:131:7) 在process._tickCallback(内部/进程/next_tick.js:180:9) 消息:“路径name
是必需的。”, 名称:“ ValidatorError”, 属性:[对象], 种类:“必填”, 路径:“名称”, 值:未定义, 原因:未定义, [Symbol(mongoose:validatorError)]:true},