Closed AndyW22 closed 3 years ago
Hmm, I see what you mean about the docs. What I did find is this BrowserStorageCache util that seems to do what the docs suggest. I'll see if I can find out more about this.
https://github.com/aws-amplify/amplify-js/blob/main/packages/cache/src/BrowserStorageCache.ts#L357
edit: updated AsyncStorageCache to BrowserStorageCache
Seems like if you console log the resolved promise, you get the value returned in the callback but an empty object is being set as the value in the cache.
Try changing your code to this
const result = Cache.getItem("key", {
callback: () => "value",
});
console.log(result);
The callback should not return a promise, getItem
doesn't handle async functions.
Try changing your code to this
const result = Cache.getItem("key", { callback: () => "value", }); console.log(result);
The callback should not return a promise,
getItem
doesn't handle async functions.
thanks that seems to fix it, would be great if amplify supported async functions for this as i imagine most of the time people will be adding data returned from an async function to the cache. This does work the long way (getting cache, checking if its null, getting the data, then setting that as the cache) since the cache itself doesn't involve any async functions then.
Might be a good feature request or topic for discussion! And no problem, glad we could help 😄
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help
channels or Discussions for those types of questions.
Before opening, please confirm:
JavaScript Framework
React, Next.js
Amplify APIs
Cache
Amplify Categories
Not applicable
Environment information
Describe the bug
Cache.getItem('key', { callback: callback });
does not exist. see https://github.com/aws-amplify/amplify-js/blob/a047ce73/packages/cache/src/Utils/CacheUtils.ts#L89This is mentioned in the docs, and the types are there, but is not usable and will just return an empty object.
Expected behavior
Callback function exists and works as stated in the docs.
Reproduction steps
Use the callback function with the cache like so:
The result will be an empty object
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response