-1

I am trying to add an object to an array of components. But this components array is in a "scenes" array inside a projects object. Here is my broken route and a model of projects to show where all the fields sit in the model. I can't seem to figure this out and it's taken me hours to diagnose the issue.

{
    "_id": {
        "$oid": "5c01c26ec028296289f53b00"
    },
    "update": false,
    "publishStatus": "draft",
    "user": {
        "$oid": "5bee30aed343c30016a664b5"
    },
    "projectName": "Griffith Uni",
    "description": "sdaidjasidjasidjiasdjsad",
    "scenes": [
        {
            "_id": {
                "$oid": "5c01c29dc028296289f53b02"
            },
            "sceneName": "Persona Choose",
            "components": []
        },
        {
            "_id": {
                "$oid": "5c01c2acc028296289f53b04"
            },
            "sceneName": "Home Menu",
            "components": []
        },
        {
            "_id": {
                "$oid": "5c0208b16c550072b6b3b499"
            },
            "sceneName": "The Smiths",
            "components": []
        }
    ],
    "lastUpdated": {
        "$date": "2018-11-30T23:06:22.173Z"
    },
    "__v": 0
}

router.post(
  "/projects/scenes/components/new",
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    const newComp = new Components({
      content: req.body.content
    });
    Project.findById(
      { _id: req.body.projectID },
      Project.findByIdAndUpdate(
        { _id: req.body.sceneID },
        { $push: { components: newComp } },
        () => res.json(newComp)
      )
    );
  }
);
YakovL
  • 7,557
  • 12
  • 62
  • 102
  • There are 3 `components`, you have to specify your question. Add to all? 1st? 2nd? – Solo Dec 01 '18 at 05:07
  • you should probably clarify the terms you use: "project" is the object which you have shown? "projects" is the name of the collection in your DB? – YakovL Dec 01 '18 at 10:35

1 Answers1

0

If I am understanding correctly you can do so with the following code.

 router.post(
   "/projects/scenes/components/new",
   passport.authenticate("jwt", { session: false }),
   async (req, res) => {
     const newComp = new Components({
       content: req.body.content
     });

     await Project.findOneAndUpdate(
       { _id: req.body.projectID, "scenes._id": req.body.sceneID },
       {
        $push: { "scenes.$.components": newComp }
       }
     );

    res.json(newComp);
  }
);

a similar issue

idkxd
  • 101
  • 2
  • { "_id": { "$oid": "5c01c26ec028296289f53b00" }, "update": false, "publishStatus": "draft", "user": { "$oid": "5bee30aed343c30016a664b5" }, "projectName": "Griffith Uni", "description": "sdaidjasidjasidjiasdjsad", "lastUpdated": { "$date": "2018-11-30T23:06:22.173Z" }, "__v": 0, "scenes": [ { "components": [], "_id": { "$oid": "5c0343c694f36e885fef4cc8" }, "sceneName": "Choose Persona" } ] } `code` – exodus2017 Dec 02 '18 at 06:17
  • mock data of a project object – exodus2017 Dec 02 '18 at 06:18
  • @exodus2017 Does the above code not complete your goal? I created a model that matches yours and the handler does what I expect it to. – idkxd Dec 02 '18 at 06:45