I am still green to Angular, I hope this makes sense. I am working with a third party api in my Ionic/Angular app and trying to fetch the record ID from a JSON array but it grabs the ID of the array index. In my code and screenshot below, I can successfully grab the shipperAddress, loadId objects etc, but not the ID object of the record. I am look for a way to loop through all the records after the index so I can grab the record id.
Edit
It just came to me that I had some objects out of order. In my model below, I added a an _ to the first ID field to make it _id which becomes my key then added a second field for ID.
load.model.ts
import {LoadLocation} from './location.model';
export class Load {
constructor(
public _id: string,
public id: string,
public userId: string,
public status: string,
public type: string,
public totalWeight: string,
public equipment: string,
public miles: string,
public shipperAddress: string,
public loadId: string,
public imageUrl: string,
public pickupDate: string,
public dropDate: string,
public delivery: string,
public notes: string,
public handler: string,
public location: LoadLocation
) {
}
}
loads.service.ts
...
fetchLoads() {
return this.authService.token.pipe(
take(1),
switchMap(token => {
const httpHeaders = new HttpHeaders ({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
});
return this.http.post<{ [key: string]: LoadData }>(
`${this.AUTH_SERVER_ADDRESS}/1.0/quotes`, {'loadId': '6'}, { headers: httpHeaders }
);
}),
map(resData => {
const loads = [];
console.log(resData);
for (const key in resData) {
if (resData.hasOwnProperty(key)) {
loads.push(
new Load(
key,
resData[key]._id,
resData[key].userId,
resData[key].status,
resData[key].type,
resData[key].totalWeight,
resData[key].equipment,
resData[key].miles,
resData[key].shipperAddress,
resData[key].loadId,
resData[key].imageUrl,
resData[key].pickupDate,
resData[key].dropDate,
resData[key].delivery,
resData[key].notes,
resData[key].handler,
resData[key].location
)
);
}
}
return loads;
// return [];
}),
tap(loads => {
this._loads.next(loads);
})
);
}
...
load.page.html
...
<ion-item *ngFor="let fbload of fbloads">
<ion-thumbnail slot="start">
<img src="../../../assets/logo_icon.jpg" alt="">
</ion-thumbnail>
<ion-label>
<h2>Load ID: {{ fbload.loadId }}</h2>
<p>
{{ fbload.shipperAddress.address1 }},
{{ fbload.shipperAddress.city }}
{{ fbload.shipperAddress.state }}
{{ fbload.shipperAddress.zip }}
</p>
</ion-label>
<ion-button
fill="clear"
color="primary"
[routerLink]="['/', 'loads', 'tabs', 'fbloads', 'edit', fbload.id]"
*ngIf="!fbload.handler">
View Load
</ion-button>
</ion-item>
...
