nuxt-modules / sanity

Sanity integration for Nuxt
https://sanity.nuxtjs.org
MIT License
217 stars 33 forks source link

Issue with Nuxt Server API Call "Cannot read from private field" #979

Closed CalebKester closed 5 months ago

CalebKester commented 6 months ago

I recently updated my packages from 1.8.1 to latest and found out that in 1.9 I am unable to make calls from a file in server/api due to the following issue. Did something change on how I need to make this call from nuxt server?

Error:

at __privateGet (./node_modules/.pnpm/@sanity+client@6.15.7/node_modules/@sanity/client/src/users/UsersClient.ts:35:3)
at Object.fetch (./node_modules/.pnpm/@sanity+client@6.15.7/node_modules/@sanity/client/src/SanityClient.ts:832:9)
at Object.handler (./server/api/footer.ts:10:1)
at ./node_modules/.pnpm/h3@1.11.1/node_modules/h3/dist/index.mjs:1890:43
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ./node_modules/.pnpm/h3@1.11.1/node_modules/h3/dist/index.mjs:1962:19
at async Object.callAsync (./node_modules/.pnpm/unctx@2.3.1/node_modules/unctx/dist/index.mjs:72:16)
at async Server.toNodeHandle (./node_modules/.pnpm/h3@1.11.1/node_modules/h3/dist/index.mjs:2249:7)

File: /server/api/footer.ts

export default defineEventHandler(async (event) => {
  const query = groq`*[_type == 'page' && defined(slug)]{
        _id,
        title,
        'url': '/' + slug.current,
    }`;

  const sanity = useSanity();

  const data = await sanity.fetch(query);

  return data;
});

Package.json

  "dependencies": {
    "@nuxtjs/sanity": "^1.10.1",
    "nuxt": "^3.11.1",
    "vue": "^3.4.21",
    "vue-router": "^4.3.0"
  }

Nuxt Config:

export default defineNuxtConfig({
  devtools: { enabled: true },

  nitro: {
    preset: "azure",
  },
  modules: ["@nuxtjs/sanity"],
  sanity: {
    projectId: "xxxxxx",
    dataset: "production",
    apiVersion: "2021-10-21",
  },
});
danielroe commented 6 months ago

Try setting minimal: true explicitly - it looks like previously you might have been relying on it falling back to minimal client as you had no @sanity/client installed.

CalebKester commented 6 months ago

I added this to my package "@sanity/client": "^6.15.7", in my sandbox repo (it was in my main repo that has this issue) and it's still having the issue.

However minimal: true does make it work both with the sanity client added and without it.

Thanks for the quick help!

danielroe commented 6 months ago

We still need to investigate why this is failing with the sanity client. Do you have any more logs or a reproduction?

CalebKester commented 6 months ago

I've moved my sandbox repo over to github and you can see it here https://github.com/CalebKester/nuxt-sanity-server . You can test it with the url http://localhost:3000/api/footer and toggling minimal on and off will error it.

plcdnl commented 5 months ago

Same error!

stephanepericat commented 5 months ago

same issue here

plcdnl commented 5 months ago

@stephanepericat as workaround you can use minimal: true

stephanepericat commented 5 months ago

@plcdnl reverting to 1.8.3 also solves the issue

xuzuodong commented 5 months ago

According to the error message and related files in the error stack, this is may be because, somewhere in v1.9.0 updates, SanityClient is been wrapped by a Proxy, and in such case, SanityClient can no longer visit its private field under the hood.

Reference: https://github.com/evanw/esbuild/issues/1969#issuecomment-1023787162

xuzuodong commented 5 months ago

Turns out this line breaks using useSanity from server.