1

I was reading on how it is possible to update existing query data with Query Client's setQueryData method.

Here is how I transform my data.

    {
      select: (data) => {
        let fullDaysArray = [] as Day[];
        data.docs.map((docSnapshot) => {
          const { id } = docSnapshot;
          let data = {
            ...docSnapshot.data(),
            documentId: id,
          } as Day;
          fullDaysArray.push(data);
        });
        fullDaysArray.sort((a, b) => a.order - b.order);
        return fullDaysArray;
      },

I did it like this:

      queryClient.setQueryData("currentDiary", newData);

However I started getting these errors:

TypeError: Cannot read properties of undefined (reading 'map')

And that is because, the newData that I am setting is not a firebase object that looks like this: Fu {_firestore: La, _userDataWriter: dh, _snapshot: qo, metadata: $u, query: Sa}, but the actual already transformed object that looks like this:

[
        {
          dayName: "Monday",
          order: 1,
          uid: "1234",
          //...

Which obviously fails data.docs.map((docSnapshot)

Is there a way for me to only update the variable that I am returning from select: (data) method, which would be fullDaysArray?

1 Answer 1

1

Is there a way for me to only update the variable that I am returning from select: (data) method, which would be fullDaysArray?

No, there is not. The data that is returned from the queryFn is what gets put into the cache, and that is also what you have to write with setQueryData. select can exist on multiple observers, and it allows you to select and subscribe to different slices of the cached data.

It might be that select is not the best place in your situation to do the transformation - if you want to transform the data that gets stored in the query cache, you have to do it inside the queryFn.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.