vercel / swr-site

The official website for SWR.
https://swr.vercel.app
Apache License 2.0
481 stars 351 forks source link

Feedback for “API": return object {data, error} is not always returned like this #539

Open kentntwari opened 1 year ago

kentntwari commented 1 year ago

I have a simple use case where I'm fetching data from an api based on the value of the router.query in NEXT JS.

    const { data, error } = useSWR(() => {
      if (router.query.sort === "shortest")
        return `http://localhost:1337/api/playlists/${options?.value}?sortBy=duration`;

      if (router.query.sort === "longest")
        return `http://localhost:1337/api/playlists/${options?.value}?sortBy=duration&order=desc`;

      if (router.query.sort === "oldest")
        return `http://localhost:1337/api/playlists/${options?.value}?sortBy=id`;

      return `http://localhost:1337/api/playlists/${options?.value}?sortBy=id&order=desc`;
    });

When an error occurs in any one of the api endpoints, instead of reading directly the error as listed above, I found that it's returned undefined but instead another {data,error} is nested inside the data that is first returned which is different from what the docs describes. Hopefully my description is clear enough. Please clarify if I'm doing anything wrong here

kentntwari commented 1 year ago

This is what I get from the console

Capture