As you can see in the picture it saves nothing. The POST request is working, but the data which I am sending to Backend it is saving only new Object with ID but not the data which I have giving, I mean Array of Objects.
I do not have any error or something the status is OK. I am testing this with Postman.
This is the Postman Post Request
I will add some code.
This is the model.
const mongoose = require("mongoose");
const dataModelScheme = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
personalData: [{
_id: mongoose.Schema.Types.ObjectId,
title: String,
firstName: String,
lastName: String,
email: String,
birthday: String,
telephone: Number,
driveLicense: String,
status: Number,
employmentType: String,
job: String,
}],
career: [{
_id: mongoose.Schema.Types.ObjectId,
name: String,
startDate: Number,
endDate: Number,
description: String,
}],
education: [{
_id: mongoose.Schema.Types.ObjectId,
name: String,
description: String
}],
skills: [{
_id: mongoose.Schema.Types.ObjectId,
name: String,
description: String
}],
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
});
module.exports = mongoose.model('ModelSchema', dataModelScheme);
And this is the controller.
async store(req, res) {
const { user_id } = req.headers;
const user = await User.findById(user_id);
if (!user) {
return res.status(400).json({ error: 'User does not exist'});
}
const modelData = new ModelData({
_id: new mongoose.Types.ObjectId(),
user: user_id,
personalData: [{
_id: new mongoose.Types.ObjectId(),
title: req.body.personalData.title,
firstName: req.body.personalData.firstName,
lastName: req.body.personalData.lastName,
email: req.body.personalData.email,
birthday: req.body.personalData.birthday,
telephone: req.body.personalData.telephone,
driveLicense: req.body.personalData.driveLicense,
status: req.body.personalData.status,
employmentType: req.body.personalData.employmentType,
job: req.body.personalData.job,
}],
skills: [{
_id: new mongoose.Types.ObjectId(),
name: req.body.skills.name,
description: req.body.skills.description,
}],
education: [{
_id: new mongoose.Types.ObjectId(),
name: req.body.education.name,
description: req.body.education.description,
}],
});
modelData.save().then(result => {
res.status(201).json(result);
console.log(req.body.personalData, "req.body");
console.log(result, "result");
}).catch(error => {
res.status(500).json({error: error});
});
},