Friday 21 April 2017

how to change nodejs schema according to my requirment

i have following data i want put data like below but my schema does not allow me what i do, to send data like below please check. when i put only single {} body its working fine but i want more then one bodies

    //Request body
            {
            "title" : "test10",
            "sch_start": "2017-04-3",
            "sch_end":"2017-04-3"
        },
        {
        "title" : "test11",
        "sch_start": "2017-04-4",
        "sch_end":"2017-04-4"

        }


       import mongoose, {Schema} from 'mongoose';

    /**
     * Model to store Calendar entries of Engineer
     */
    var EngineerScheduleSchema = new Schema({
      title: { type: String, default: ''},                             
      available: { type: Boolean, default: false },
      sch_start: { type:Date, default: Date.now },
      sch_end: { type:Date, default: Date.now }
    });

    export default mongoose.model('engineer_schedule', EngineerScheduleSchema);


  //  Main Model Mongoose Schema
     schedule: [EngineerSchedule.schema]


//API Method 
export function updateSchedulecalendar(req, res) {  
  var responseSchedule;

  //Insert

    return Engineer.findOneAndUpdate({ _id: req.params.id }, { $addToSet: { schedule: req.body } }, { new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec()
      .then((entity) => {
        if (entity) {
          responseSchedule = entity.schedule;
          return EngineerEvents.emit(engineerEvents.updatedSchedule, req.user, entity);
        }
        else {
          return res.status(404).end();
        }
      })
      .then(()=> { return res.status(200).json(responseSchedule); })
      .catch(handleError(res));
  }



via Addi Khan

No comments:

Post a Comment