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

Sanity client: Expected 0 arguments, but got 1 #882

Open
avishkakavindu opened this issue Aug 18, 2024 · 4 comments
Open

Sanity client: Expected 0 arguments, but got 1 #882

avishkakavindu opened this issue Aug 18, 2024 · 4 comments

Comments

@avishkakavindu
Copy link

Describe the bug

I have created a sanity client by using createClient() but when I tried to use it to fetch data I'm getting Expected 0 arguments, but got 1 for fecth() method.

To Reproduce

Steps to reproduce the behavior:

I'm using typescript "typescript": "^4.9.5" with react.
I have defined the client like this. in sanity.ts file.

import { createClient } from '@sanity/client';
import { getSanityConfigs } from '@configs/index';

// get sanity configs from `.env`
const { projectId, dataset, useCdn, apiVersion } = getSanityConfigs();
export const cmsClient = createClient({
  projectId,
  dataset,
  useCdn, // set to `false` to bypass the edge cache
  apiVersion, // use current date (YYYY-MM-DD) to target the latest API version
});

(the configurations are there. Confirmed by console log)

And I have used the client in sanity.service.ts

import { cmsClient } from '@api/sanity';

export const getSectionData = async () => {
  const query: string = '*[_type = "section"]';
  const sectionData = await cmsClient.fetch<any>(query);
  return sectionData;
};

but I'm getting

ERROR in ./src/services/sanity.service.ts:5:50
TS2554: Expected 0 arguments, but got 1.
    3 | export const getLandingPageSectionData = async () => {
    4 |   const query: string = '*[_type = "section"]';
  > 5 |   const sectionData = await cmsClient.fetch<any>(query);
      |                                                  ^^^^^
    6 |   return sectionData;
    7 | };
    8 |

Found 1 error in 11256 ms.

Expected behavior

Able to fetch data.

Which versions of Sanity are you using?

"@sanity/client": "^6.21.3"

What operating system are you using?
Windows 11

Which versions of Node.js / npm are you running?

Node v20.11.1
Npm v10.8.1

@avishkakavindu
Copy link
Author

avishkakavindu commented Aug 19, 2024

I tried with older version "@sanity/client": "^5.4.2" it worked, then I tested with "@sanity/client": "^6.21.1" it has the same issue. Problem was with v6.2x.x. (I did remove node_modules and package-lock.json, tries reopening vscode, when testing the v6.21.3.
Cheers

@cadyfatcat
Copy link

I am also experiencing the same issue with fresh install of 6.21.3.

@joeboylson
Copy link

@cadyfatcat I was experiencing this with v6.21.3 as well!

However, I managed to get things working on my end by going back a version to v6.1.4.

@stipsan
Copy link
Member

stipsan commented Nov 5, 2024

Could you try using casting instead of the generic?:

-const sectionData = await cmsClient.fetch<any>(query);
+const sectionData = await cmsClient.fetch(query) as Promise<any>;

Although you shouldn't need to cast to any at all as that's the default value:

-const sectionData = await cmsClient.fetch<any>(query);
+const sectionData = await cmsClient.fetch(query);

The generics for client.fetch changed to accommodate TypeGen

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

4 participants