我一直在寻找解决问题的多种途径,但不幸的是我找不到答案。你能帮我吗。
其中包含带有架构的文件。
const mongoose = require('mongoose');
const config = require('../config/database');
const analogueNumberSchema = new mongoose.Schema({
analogueName: String,
analogueNum: String
});
const detailSchema = new mongoose.Schema({
detailName: String,
originalNumber: [String],
analogueNumber: [analogueNumberSchema],
note: String,
picture: String
});
const modelModificationsSchema = new mongoose.Schema({
modelModificationName: String,
modelDetails: {
engine: [detailSchema],
transmission: [detailSchema],
frontAxle: [detailSchema],
rearAxle: [detailSchema],
breakes: [detailSchema],
electrics: [detailSchema]
}
});
const markModelsSchema = new mongoose.Schema({
markModelName: String,
modelModifications: [modelModificationsSchema]
});
const carsSchema = new mongoose.Schema({
markName: String,
markModels: [markModelsSchema]
});
const Car = module.exports = new mongoose.model('Car', carsSchema);
module.exports.addCar = function (newCar, callback) {
newCar.save(callback);
};
这是路由器代码:
router.post('/post', (req, res, next) => {
let newCar = new Car({
markName: req.body.markName,
markModels: [{
markModelName: req.body.markModelName,
modelModifications: [{
modelModificationName: req.body.modelModificationName,
modelDetails: [{
engine: [{
detailName: req.body.detailName,
originalNumber: req.body.originalNumber.split(","),
analogueNumber: [{
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNumber
}],
note: req.body.note,
picture: req.body.picture
}],
transmission: [{
detailName: req.body.detailName,
originalNumber: req.body.originalNumber.split(","),
analogueNumber: [{
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNumber
}],
note: req.body.note,
picture: req.body.picture
}],
frontAxle: [{
detailName: req.body.detailName,
originalNumber: req.body.originalNumber.split(","),
analogueNumber: [{
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNumber
}],
note: req.body.note,
picture: req.body.picture
}],
rearAxle: [{
detailName: req.body.detailName,
originalNumber: req.body.originalNumber.split(","),
analogueNumber: [{
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNumber
}],
note: req.body.note,
picture: req.body.picture
}],
breakes: [{
detailName: req.body.detailName,
originalNumber: req.body.originalNumber.split(","),
analogueNumber: [{
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNumber
}],
note: req.body.note,
picture: req.body.picture
}],
electrics: [{
detailName: req.body.detailName,
originalNumber: req.body.originalNumber.split(","),
analogueNumber: [{
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNumber
}],
note: req.body.note,
picture: req.body.picture
}]
}]
}]
}]
});
Car.addCar(newCar, (err, car) => {
if (err) {
res.json({success: false, msg: 'failed to add new car'});
console.log(err);
} else {
res.json({success: true, msg: 'new car added'});
}
});
});
这是“邮递员”中的后查询代码
{
"markName": "Daewoo",
"markModels": [{
"markModelName": "Nexia",
"modelModifications": [{
"modelModificationName": "1.5 8v",
"modelDetails": [{
"engine":[{
"detailName": "fdkj",
"originalNumber": ["123", "4545"],
"analogueNumber": [{
"analogueName": "sdkfd",
"analogueNum": "csdfs"
},
{
"analogueName": "sdd",
"analogueNum": "csdfsasdas"
}],
"note": "csdsldkf;sks;dskjfsndjfns",
"picture": ""
}],
"transmission": [{
"detailName": "fdkj",
"originalNumber": ["123", "4545"],
"analogueNumber": [{
"analogueName": "sdkfd",
"analogueNum": "csdfs"
},
{
"analogueName": "sdd",
"analogueNum": "csdfsasdas"
}],
"note": "csdsldkf;sks;dskjfsndjfns",
"picture": ""
}],
"frontAxle": [{
"detailName": "fdkj",
"originalNumber": ["123", "4545"],
"analogueNumber": [{
"analogueName": "sdkfd",
"analogueNum": "csdfs"
},
{
"analogueName": "sdd",
"analogueNum": "csdfsasdas"
}],
"note": "csdsldkf;sks;dskjfsndjfns",
"picture": ""
}],
"rearAxle": [{
"detailName": "fdkj",
"originalNumber": ["123", "4545"],
"analogueNumber": [{
"analogueName": "sdkfd",
"analogueNum": "csdfs"
},
{
"analogueName": "sdd",
"analogueNum": "csdfsasdas"
}],
"note": "csdsldkf;sks;dskjfsndjfns",
"picture": ""
}],
"breakes": [{
"detailName": "fdkj",
"originalNumber": ["123", "4545"],
"analogueNumber": [{
"analogueName": "sdkfd",
"analogueNum": "csdfs"
},
{
"analogueName": "sdd",
"analogueNum": "csdfsasdas"
}],
"note": "csdsldkf;sks;dskjfsndjfns",
"picture": ""
}],
"electrics": [{
"detailName": "fdkj",
"originalNumber": ["123", "4545"],
"analogueNumber": [{
"analogueName": "sdkfd",
"analogueNum": "csdfs"
},
{
"analogueName": "sdd",
"analogueNum": "csdfsasdas"
}],
"note": "csdsldkf;sks;dskjfsndjfns",
"picture": ""
}]
}]
}]
}]
}
并且有错误消息:
'对值的嵌入转换失败“ [{引擎:[[Object]],\ n
传输:[[Object]],\ n frontAxle:[[Object]],\ n
RearAxle:[[Object]],\ n中断:[[Object]],\ n电子设备:[ [Object]]}]“在路径“ modelDetails”处, 名称:“ CastError”, stringValue:'“ [{引擎:[[Object]],\ n传输:[[Object]],\ n frontAxle:[[Object]],\ n RearAxle:[[Object] ],\ n中断:[[Object]],\ n电子设备:[[Object]]}]“', 种类:“嵌入式”, 值:[Array], 路径:“ modelDetails”, 原因:[Object]}},_ message:“汽车验证失败”,名称:“ ValidationError”}
非常感谢您的帮助。谢谢!
答案 0 :(得分:0)
最后我得到了我的问题的答案。名册代码有误。您只需要定义属性而不定义结构即可。这是代码
router.post('/post', (req, res, next) => {
let newCar = new Car({
markName: req.body.markName,
markModels: req.body.markModels,
modelModificationName: req.body.modelModificationName,
modelDetails: req.body.modelDetails,
engine: req.body.engine,
transmission: req.body.transmission,
frontAxle: req.body.frontAxle,
rearAxle: req.body.rearAxle,
breakes: req.body.breakes,
electrics: req.body.electrics,
detailName: req.body.detailName,
originalNumber: req.body.originalNumber,
analogueNumber: req.body.analogueNumber,
note: req.body.note,
picture: req.body.picture,
analogueName: req.body.analogueName,
analogueNum: req.body.analogueNum
});
Car.addCar(newCar, (err, car) => {
if (err) {
res.json({ success: false, msg: 'failed to add new car' });
console.log(err);
} else {
res.json({ success: true, msg: 'new car added' });
}
});
}); 如果有人感兴趣,谢谢。抱歉文本中的错误))