I am attempting to send a GET request to my backend API that will return an array of objects. The code I am using is as follows:
small.component.ts (when the function openDepositModal() is called, it triggers the function getUserInventory() from auth.service.ts, which then sends a GET request to the backend API and retrieves an array of objects.)
[...]
export class Item {
id: String;
name: String;
img: String;
value: String;
}
const items: Item[] = [];
[...]
openDepositModal() {
if (!items) {
this.authService.getUserInventory().subscribe(data => {
items = data; <-- ERROR OCCURS HERE
});
}
}
auth.service.ts
[...]
getUserInventory() {
let headers = new Headers();
this.loadToken();
headers.append('Authorization', 'JWT ' + this.authToken);
headers.append('Content-Type', 'application/json');
return this.http.get('http://localhost:3000/api/user/inventory', { headers: headers })
.map(res => res.json());
}
[...]
While working inside small.component.ts, I am trying to populate the "items" array with the data retrieved from the service. However, I encounter the error "cannot assign to array because it is a constant or a read-only property". Can someone please help me fix this issue in my code? Thank you. :-)