I want to give a post request using my API. The JSON is like this:
{
"_id": "56b8e96xxxxxxxxxx7cd",
"name": "abc",
"conditions": [
{
"name": "Condition 1"
},
{
"name": "Condition 2"
}
],
"id": 10
}
The following is the angular code:
app.factory('Cohort', function($resource) {
return $resource('http://API URL:id')
});
I am using the following angular function to give the post request:
$scope.createCohort= function (){
var arr=["condition 1","condition 2"];
var cohort=new Cohort();
cohort.name=$scope.CohortName;
cohort.id=$scope.CohortId;
for(var i=0;i<length_of_arr;i++)
{
cohort.conditions[i].name=arr[i] // This line gives me error.
}
Cohort.save(cohort,function(){
});
};
The error is
Cannot read property '0' of undefined
How can I give the post request to it ?