0

When I attempt to do so I the rest to the data is posted but the array remains empty

my Schma looks like this

const mongoose = require('mongoose');

const DocumentSchema = mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'users',
  },
  dateCreated: String,
  title: String,
  // ID of the users that created the document
  creatorId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'users',
  },






  comment: [{ title: String, detail: String }]



});

module.exports = mongoose.model('myDocument', DocumentSchema);

My Post script in postman looks like this

{
    "label":"Got More data",
    "linkUrl":"www.google.com",
    "title":"test 14",
    "comment":[
        {
            "title":"hello",
            "detail":"some detail"
        }]
}

When I get the data that I have updated it looks like this

  {
        "_id": "5eb91c0e35eecb369696b9c5",
        "title": "test 14",
        "creatorId": "5d8856e5903a260588b77c30",
        "user": "5d8856e5903a260588b77c30",
        "dateCreated": "Mon May 11 2020 10:34:06 GMT+0100 (British Summer Time)",
        "comment": [],
        "__v": 0
    }

OTHER THINGS I HAVE TRIED

I have tried changing the schema definition according to this post on Stack overflow How to define object in array in Mongoose schema correctly with 2d geo index

to this

comment : [{
    title : String,
    detail : String
     }]

and this approach too

 comment :  {
             "title":"A title",
             "detail": "some random comment"
           }

but I still get a blank array

Benny
  • 103
  • 2
  • 12

1 Answers1

0

The problem was in the react api setup I had not added the filed to the api code below is the new code with the "comment" field included (under imageUrl ) . It works now

router.post(
  '/',
  [
    auth,
    [
      check('title', ' myDocument Title is required')
        .not()
        .isEmpty()
    ]
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    const { title, summary, imageURL, template, comment } = req.body;

    try {
      const newMyDocument = new MyDocument({
        title,
        summary,
        imageURL,
        comment,
        template,
        creatorId: req.user.id,
        user: req.user.id,
        columnOrder:["col1","col2","col3"],
        columns:[
          {col_1 :
            {
              id:"col_1",
              title:"facts",
              taskIds:["test1","test2"]
            }
          },
          {col_2 :
            {
              id:"col_2",
              title:"Pros",
              taskIds:["test1","test2"]
            }
          },
          {col_3 :
            {
              id:"col_3",
              title:"Cons",
              taskIds:["test1","test2"]
            }
          }
        ],
        dateCreated:new Date()
      });
      const myDocument = await newMyDocument.save();
      ///this is the request to othe server

      res.json(myDocument);
    } catch (err) {
      res.status(500).send('Server Error 76');
    }
  }
);
Benny
  • 103
  • 2
  • 12