2

I've been having a problem with saving data from an array object to the MongoDB database. I have my schema for the main document with embedded subdocuments. The saves for the main document and all the other subdocuments were successful except for a subdocument which is an array.

As you can see from the screenshot, the fields from the subdocuments are not being saved to the database.

Screencap from Robo 3T

Any help would be much appreciated. Thanks!

Sample request object:

{
  "incidentNumber": "IN1001",
  "status": "New"
  "Approval": {
    "approvers": [
      {
        "approverType": "Asset Management",
        "approverName": "Bob",
        "approvalDate": 1234
      },
      {
        "approverType": "HR",
        "approverName": "Janet",
        "approvalDate": 1234
      },
      {
        "approverType": "Finance",
        "approverName": "Bill",
        "approvalDate": 1234
      }
    ]
  }
}

Schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const approvalSchema = new Schema(
  {
    approvers: [
      {
        approverType: {
          type: String
        },
        approverName: {
          type: String
        },
        approvalDate: {
          type: Date
        }
      }
    ]
  }
);

const headerSchema = new Schema(
  {
    incidentNumber: {
      type: String,
      required: true,
      unique: true
    },
    status: {
      type: String,
      required: true,
      enum: ['Completed', 'Draft', 'New', 'Pending', 'Submitted']
    },
    approval: [approvalSchema]
  },
  { timestamps: true }
);

Server code:

let incidentNumber: "IN1001";
let status: "New";

app.post('/create-incident', (req, res) => {
  const header = new Header({
    incidentNumber: incidentNumber,
    status: status,
    approval: [
      {
        approvers: [
          {
            approverType: req.body.Approval.approvers.approverType,
            approverName: req.body.Approval.approvers.approverName,
            approvalDate: req.body.Approval.approvers.approvalDate
          }
        ]
      }
    ]
  });
});

header
  .save()
  .then(Header => {
    res.status(200).send(Header);
  })
  .catch(e => {
    res.status(400).send(e);
  }
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • 1
    Just assign `approval: req.body.Approval`. It's an "array", so everything after that point needs an "index" to access the elements in the object. Without that the result is `null`, but you want to add the whole array anyway, so just do that. – Neil Lunn Jun 09 '18 at 13:54
  • [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Neil Lunn Jun 09 '18 at 13:56
  • Your code seems fine to me as I ran it, the only problem that might be in your req body. Your `req.body.Approval.approvers.approverType` body may be returning `undefined` – Sachin Bhandari Jun 09 '18 at 14:34
  • @NeilLunn I followed your advice by just putting req.body.Approval, and it magically worked! Actually, the first time I did that, it didn't work (maybe because of the nested square brackets and curly braces, I don't know), so what I did was instead of mapping field by field, I just mapped by subdocument all throughout. It really reduced my verbose code to just a few lines. Thanks for this wonderful tip! Thanks also, Sachin, for taking time to look into my problem. You guys are amazing. – jake2point0 Jun 09 '18 at 15:21

0 Answers0