Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array-based entity fetched from cache parsed as object instead of array #45

Open
herrherrmann opened this issue Jan 13, 2020 · 0 comments
Labels

Comments

@herrherrmann
Copy link
Collaborator

herrherrmann commented Jan 13, 2020

If the api in a ladda-wrapped call returns an array, only the initial call (that does the actual request) returns the array as expected. Any subsequent call (that's fetching the entity from the cache) will return the array parsed an object instead.

It's surprising that the type changes "on the fly" and ladda should probably return the entities more consistently (ideally the way they came from the backend).

Example

Initial ladda call returns:

[{ id: 'asd', name: 'Banana' }, { id: 'tzu', name: 'Pomegrenade' }]

Second and subsequent ladda calls return:

{ 0: { id: 'asd', name: 'Banana' }, 1: { id: 'tzu', name: 'Pomegrenade' } }

Current workaround

A workaround is to wrap the ladda call in another function that extracts the values from the response if it's an object:

export const getEntitiesAsArray = async (id: string) => {
    const entities = await api.myEntities.getAllForId(id);
    // Because ladda parses arrays as objects when returning from cache, we need to retransform to an array here.
    return typeof entities === 'object' ? Object.values(entities) : entities;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant