0

I am a newbie to Angular and I am trying to implement a calendar for events. I am using Angular UI Calendar. When I post the data, it gets posted but it gives an error.

POST METHOD

 $http({
            method : 'POST',
            url : '/api/appointment',
            data : $scope.appointment
        }).success(function(data) {
            console.log(data);
        }).error(function(err) {
            console.log(err);
        }); 

Expected array but received:

{  "__v": 0,
  "visitType": "Referral",
  "timeDuration": "900000",
  "patientId": "BMSHT002",
  "doctorId": "BMSHTDOC60",
  "_id": "5833da8d9889edd037a54c30",
  "eventSources": [{
    "title": "Patient Name : Vignesh\t   Age :10\t    Gender : male\t   Email Id : bvikcy.2205@gmail.com\n Appointment Type : Referral\n Doctor Name : Demo 1",
    "start": "2016-11-21T04:30:00.000Z",
    "end": "2016-11-21T04:45:00.000Z",
    "_id": "5833da8d9889edd037a54c31"
  }]
}

I know that this is a JSON Data, But I am not quite sure on how to resolve this issue. Could someone please help me resolve the issue?

This is the schema that I am using while posting the data

var appointmentSchema = mongoose.Schema({
  patientId : String,
  doctorId : String,
  visitType : String,
  timeDuration : String,
  eventSources: [{
    title : String,
    start : Date,
    end : Date
  }]
});

Node JS CODE

router.route('/appointment')
            .post(function(req, res) {
                var appointmentData = req.body;
                var appointments = new Appointment(appointmentData);
                console.log(appointments);

                appointments.save(function(err, appointment) {                                       
                    if (err)
                        throw err;
                    res.send(appointment);
                })
            })

Thanks in Advance

Vignesh
  • 131
  • 2
  • 5
  • 13
  • Just to clarify; are you sending the apointmentSchema via Angular UI Calendar? Also, perhaps edit your post including the actual post method. – onmyway Nov 22 '16 at 06:58
  • Post the input you are passing from Angular and the nodejs code to build the appointment object? – Aruna Nov 22 '16 at 07:09
  • @onmyway - The schema is from mongooseJS. This is schema through which the data would be posted on to my db which works absolutely fine. – Vignesh Nov 22 '16 at 07:32
  • @Aruna. I have added my nodejs Code and the inputs are the ones that are shown in the error (visitType, TimeDuration ...) – Vignesh Nov 22 '16 at 07:35
  • It seems like you are posting an object {} and not an array[]. Perhaps change this to an array, as this is what is expected. – onmyway Nov 22 '16 at 07:46
  • Is this the above angular code, exactly throwing error? – Aruna Nov 22 '16 at 07:47
  • @Vignesh can you try my below suggestion? – Aruna Nov 22 '16 at 07:49

1 Answers1

0

Instead of 'res.send', try 'res.json' as below

router.route('/appointment')
            .post(function(req, res) {
                var appointmentData = req.body;
                var appointments = new Appointment(appointmentData);
                console.log(appointments);

                appointments.save(function(err, appointment) {                                       
                    if (err)
                        throw err;
                    res.json(appointment);
                })
            })
Aruna
  • 11,959
  • 3
  • 28
  • 42