Vignesh Vignesh - 10 days ago 5
AngularJS Question

Expected Array but received

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

Answer

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);
                })
            })
Comments