1

does anyone know how I can add an object to an array called worker_info without "replacing" all the information it contains? (I'm learning the mean stack) I have this code but what it does is replace all the information and erase the other data I already had. With the code I got this, summarizing what I want is that the worker_info array be added the worker object without deleting the rest, I would appreciate if someone can help me, any advice would be useful, thank you

I tried this

 User.findOneAndUpdate(serviceId, { "$push": { services: { worker_info: worker } } }, { new: true }, (err, us) => {

res.status(200).send(us);
                    });

I expected something like this

{
    "services": [
        {
            "worker_info": [],
            "_id": "5ce485a3288efe1834d4d71c",
            "user_address": "dbarva252",
            "description_service": "descripcion servicio",
            "skill_type": "wasting time",
            "amount_money": 0
        },
        {
            "worker_info": [{
                "_id": "5ce4867e288efe1834d4d71e",
                "name": "Trabajador",
                "surname": "arreglador",
                "identification": "123457669",
                "birthdate": "1 febrero 1999",
                "skills": "time waster fixer",
                "image": null,
                "__v": 0
            }],
            "_id": "5ce485c5288efe1834d4d71d",
            "user_address": "esta es una direccion",
            "description_service": "descripcion servicio 2",
            "skill_type": "wasting time part 2",
            "amount_money": 0
        }
    ],
    "_id": "5ce48569288efe1834d4d71a",
    "user": "usuario123",
    "name": "mauricio",
    "surname": "barva",
    "identification": "123123",
    "birthdate": "6 febrero 2000",
    "city": "city",
    "address": "tv 1d norte",
    "cellphone": "123",
    "image": null,
    "status": false,
    "__v": 0
}

but I got this

{
    "services": [
        {
            "worker_info": [],
            "_id": "5ce485a3288efe1834d4d71c",
            "user_address": "dbarva252",
            "description_service": "descripcion servicio",
            "skill_type": "wasting time",
            "amount_money": 0
        },
        {
            "worker_info": {
                "_id": "5ce4867e288efe1834d4d71e",
                "name": "Trabajador",
                "surname": "arreglador",
                "identification": "123457669",
                "birthdate": "1 febrero 1999",
                "skills": "time waster fixer",
                "image": null,
                "__v": 0
            }
        }
    ],
    "_id": "5ce48569288efe1834d4d71a",
    "user": "usuario123",
    "name": "mauricio",
    "surname": "barva",
    "identification": "123123",
    "birthdate": "6 febrero 2000",
    "city": "city",
    "address": "tv 1d norte",
    "cellphone": "123",
    "image": null,
    "status": false,
    "__v": 0
}

As you can see, it replaces all the information.

0 Answers0