infinum/ngx-hal

find not returning models

Closed this issue · 7 comments

when running find({}) with or without metadata, the info is not being returned. The issue seems to be on HalDocument, the api it's returning:
image

but HalDocument is looking for _embedded.item instead of _embedded.product or which ever my model name is.
Not sure how to fix it, but maybe the issue is on getListPropertyName .

yboug commented

Hello i have found the same error, getListPropertyName this method implementation is defaulting value to 'item' i think we should use the the property model name to retrieve data

yboug commented

@dfliess did you found a workarround?

For now I created a temporary HalDocument class:

‘’’
//Fix bug not getting _embedded.[model_type] on find
class FixHalDocument extends HalDocument {

constructor(
    private _rawResource: RawHalResource,
    private _rawResponse: HttpResponse<any>,
    private _modelClass: ModelConstructor<HalModel>,
    private _datastore: DatastoreService
  ) {
    
    super(_rawResource, _rawResponse, _modelClass, _datastore);
    this.parseRawResources2(_rawResource);
  }
  private parseRawResources2(resources: any): void {
    const embedded = resources[EMBEDDED_PROPERTY_NAME];
    const items: Array<any> = embedded[Object.keys(embedded)[0]];
    this.models = this.generateModels2(items);
    //this.pagination = this.generatePagination(resources);
  }
  private generateModels2(resources: Array<any>): Array<HalModel> {
    return resources.map((resource: any) => {
        return new this._modelClass(resource, this._datastore, this._rawResponse);
    });
  }
  public get hasEmbeddedItems(): boolean {
    const embedded = this._rawResource[EMBEDDED_PROPERTY_NAME];
    return this._rawResource[EMBEDDED_PROPERTY_NAME] && this._rawResource[EMBEDDED_PROPERTY_NAME][Object.keys(embedded)[0]];
  }

}

‘’’

And at the @DatastoreConfig specified

halDocumentClass: FixHalDocument

I dont lime it but its working for my needs

yboug commented

Thanks for sharing your code, customizing HalDocument should be a nice solution

@dfliess thanks for reporting the issue
@yboug thanks for the feedback

The issue should be fixed in v0.0.88.
The new version fallbacks to the first item in _embedded instead of item property.
Could you verify that this fixes your issues?

Please note that there is a breaking change in minor version v0.0.84. Arguments of model.save and model.delete methods are changed a bit, please refer to the docs

yboug commented

@safo6m tesged with v0.0.88 and worked fine

@yboug thanks.