I am trying to fetch and display data from a HashMap in my Angular application by making a GET request to a Spring Application. Here is the code I have tried:
Spring code:
@GetMapping("gateways")
public ResponseEntity<?> getGateways() {
Map<Integer, String> list = new HashMap<>();
list.put(1, "Bogus");
return ok(list.put);
}
Angular Service:
getContractGatewaysList(): Observable<Array<ContractGatewaysList>> {
return this.http.get<Array<ContractGatewaysList>>(environment.api.urls.contracts.getContractGateways);
}
Angular component:
gateways: ContractGatewaysList[];
this.contractService.getContractGatewaysList()
.subscribe(value => {
if (value != null) {
this.gateways = value;
}
});
Interface:
export interface ContractGatewaysList {
id: number;
name: string;
}
HTML code:
<div class="form-group gateway">
<div class="input-group-prepend">
<label for="merchant_id">Gateway</label>
</div>
<select class="custom-select" name="gateway" [(ngModel)]="contract.gateway" id="gateway" required>
<option selected>Please Select...</option>
<option [value]="gateway.id" *ngFor="let gateway of gateways">{{ gateway.name }}</option>
</select>
</div>
However, I am facing an issue where the list appears empty. I need help with properly converting the Java HashMap and displaying the values in the dropdown menu. Can someone provide me with a working code example as I am stuck on this problem?
The error message I receive is:
ERROR Error: Cannot find a differ supporting object '3873648042962238500' of type 'number'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3152)
at checkAndUpdateDirectiveInline (core.js:9246)
at checkAndUpdateNodeInline (core.js:10507)
at checkAndUpdateNode (core.js:10469)
at debugCheckAndUpdateNode (core.js:11102)
at debugCheckDirectivesFn (core.js:11062)
at Object.eval [as updateDirectives] (ContractNewComponent.html:50)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)
at checkAndUpdateView (core.js:10451)
at callViewAction (core.js:10692)