Reputation: 31
I've been looking for solving my problem in many sources but unfortunately I can't find an answer. Can you help me please.
There is file with schema in it.
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);
};
Here is the router code:
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'});
}
});
});
Here is the code of post-query in "Postman"
{
"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": ""
}]
}]
}]
}]
}
And there is the error message:
'Cast to Embedded failed for value "[ { engine: [ [Object] ],\n
transmission: [ [Object] ],\n frontAxle: [ [Object] ],\n
rearAxle: [ [Object] ],\n breakes: [ [Object] ],\n electrics: [ [Object] ] } ]" at path "modelDetails"', name: 'CastError', stringValue: '"[ { engine: [ [Object] ],\n transmission: [ [Object] ],\n frontAxle: [ [Object] ],\n rearAxle: [ [Object] ],\n breakes: [ [Object] ],\n electrics: [ [Object] ] } ]"', kind: 'Embedded', value: [Array], path: 'modelDetails', reason: [Object] } }, _message: 'Car validation failed', name: 'ValidationError' }
I'm very appreciate of your help. thanks!
Upvotes: 2
Views: 657
Reputation: 31
At last i have an answer of my problem. There is mistake in roter code. All you need is just define properties whithout defining structure. Here is the code
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' });
}
});
}); If anyone was interested, thankyou. Sorry for mistakes in text))
Upvotes: 1