#Getting data from localStore when using optimistic update always ends up undefined

3 messages · Page 1 of 1 (latest)

faint coral
#

const existingThing = localStore.getQuery(
api.things.getUserThing,
{
thingId: args.thingId,
userId: currentUser._id,
}
)

i get the data like that but it always ends up undefined and im not sure why

this is inside the with optimistic update attached to the mutation

the id + user id is there and correct, ive logged them

am i missing anything to setup the localstore to get it working

hollow harborBOT
#

Hi @faint coral. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The localStore.getQuery function is used to retrieve data from the local store. If it's returning undefined, it could mean that the data you're trying to access isn't loaded in the local store at the time of the optimistic update.

Here are a few things you could check:

  1. Data Loading: Ensure that the data you're trying to access with localStore.getQuery is loaded before the optimistic update is run. The data needs to be fetched and loaded into the local store before you can access it.

  2. Query Matching: The localStore.getQuery function will only return data for queries that match the name and arguments exactly. Make sure that the query name (api.things.getUserThing) and arguments ({ thingId: args.thingId, userId: currentUser._id }) match exactly with a query that has been executed.

  3. Data Existence: Check if the data you're trying to access actually exists. If the data doesn't exist, localStore.getQuery will return undefined.

If you've checked all of these and you're still having issues, it might be helpful to provide a more complete code sample or to seek help from the Convex community or support channels.

For more information, you can refer to the Convex documentation on Optimistic Updates (https://docs.convex.dev/client/react/optimistic-updates) and OptimisticLocalStore (https://docs.convex.dev/api/interfaces/browser.OptimisticLocalStore).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: