Here is an interface I have created:
export interface MyMedicine {
_id: String;
name: String;
quantity: Number;
time: String;
}
This snippet shows my Angular service used for posting data:
postMed(newMed): Observable<MyMedicine[]>{
var headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.http.post<MyMedicine[]>(`${this.url}/api/medicine`, newMed, {headers:headers});
}
The following code block demonstrates a component function addmedicine() that posts data upon form submission:
addmedicine(){
const newMedicine = {
name: this.name,
quantity: this.quantity,
time: this.time
};
this.medService.postMed(newMedicine)
.subscribe(medicine =>{
this.medicines.push(medicine);
})}
An error message is displayed stating: "Argument of type 'MyMedicine[]' is not assignable to parameter of type 'MyMedicine'. Type 'MyMedicine[]' is missing the following properties from type 'MyMedicine': _id, name, quantity, time"
The necessary imports of the interface and service have been included in the component. Dependency injection has been properly managed. Note that _id corresponds to the default id generated by MongoDB.