After exhausting all my resources on the internet and StackOverflow, I still couldn't find the right answer. This is my first project in Angular with MongoDB, but I keep encountering this error: "Type 'Promise' is not assignable to type 'Contact[]'." Can someone help me figure out how to solve this issue? contact.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {map} from 'rxjs/operators';
@Injectable()
export class ContactService {
constructor(private http: HttpClient) { }
getContacts()
{
return this.http.get('http://localhost:3000/api/contacts')
.pipe(map((res: Response) => res.json()));
}
addContact(newContact)
{
var headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/api/contact', newContact, {headers:headers})
.pipe(map((res: Response) => res.json()));
}
deleteContacts(id)
{
return this.http.delete('http://localhost:3000/api/contact'+id)
.pipe(map((res: Response) => res.json()));
}
}
contacts.component.ts
import { Component, OnInit } from '@angular/core';
import {ContactService} from '../contact.service';
import {Contact} from '../contact';
@Component({
selector: 'app-contacts',
templateUrl: './contacts.component.html',
styleUrls: ['./contacts.component.css'],
providers: [ContactService]
})
export class ContactsComponent implements OnInit {
contact:Contact;
contacts:Contact[];
first_name:string;
last_name:string;
phone:string;
constructor(private contactService: ContactService) {
var contacts;
}
ngOnInit() {
this.contactService.getContacts()
.subscribe(contacts =>
this.contacts = contacts);
}
}
contact.ts
export class Contact{
_id: string;
first_name: string;
last_name: string;
phone:string;
}