0

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 Requestenter image description here

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});
        });
    },

2 Answers 2

1

This is because you are passing only one element for personalData, skills and education when you are creating your model. Try with something like this

const modelData = new ModelData({
            _id: new mongoose.Types.ObjectId(),
            user: user_id,
            personalData: req.body.personalData.map(personalData => {
                return {
                    _id: new mongoose.Types.ObjectId(),
                    title: personalData.title,
                    firstName: personalData.firstName,
                    lastName: personalData.lastName,
                    email: personalData.email,
                    birthday: personalData.birthday,
                    telephone: personalData.telephone,
                    driveLicense: personalData.driveLicense,
                    status: personalData.status,
                    employmentType: personalData.employmentType,
                    job: personalData.job
                };
            }),
            skills: req.body.skills.map(skill => {
                return {
                    _id: new mongoose.Types.ObjectId(),
                    name: skill.name,
                    description: skill.description
                };
            },
            education: req.body.education.map(education => {
                return {
                    _id: new mongoose.Types.ObjectId(),
                    name: education.name,
                    description: education.description
                };
            }
        });
Sign up to request clarification or add additional context in comments.

3 Comments

How would be then the GET request ? something like this ? async show(req, res) { const modelData = await ModelData.find().where({"user": req.params.id}).exec(); if (!modelData) { return res.status(204).json({error: "No Data"}); } return res.status(200).send(modelData); }
You are almost right: async show(req, res) { const modelData = await ModelData.find({"user": req.params.id}).exec(); if (!modelData) { res.status(204).json({error: "No Data"}); return; } res.json(modelData); }
I do have a little question. How would be than the Interface model in Frontend. For example.
1

You are sending an array of objects, but are trying to read them as objects using dot notation. You need read them as arrays by providing the index in brackets. I won't go through all of your properties, but this is the gist of what you need to do.`

  const modelData = new ModelData({
        _id: new mongoose.Types.ObjectId(),
        user: user_id,
        personalData: [{
            _id: new mongoose.Types.ObjectId(),
            title: req.body.personalData[0].title,
            firstName: req.body.personalData[0].firstName,
            lastName: req.body.personalData[0].lastName,
          // DO similar for the rest of your properties
        }],

`

2 Comments

Yeah but what will happen if req.body.personalData[0].title it will not except 0 but 1 ?
You'll need to use .map() as suggested in another answer or loop over your array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.