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

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

Open
kentntwari opened this issue Aug 31, 2023 · 1 comment

Comments

@kentntwari
Copy link

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
Copy link
Author

This is what I get from the console

Capture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant