Error using createClient in Sanity and Typescript code
46 replies
Last updated: Mar 6, 2023
L
Hello All... new to Sanity and Typescript so there is probably an obvious answer, but I'm getting this error: TypeError: Object(...) is not a function using this codeimport { createClient } from '
export async function *getIgnoredHandles*() {
const config:
ClientConfig = { projectId: 'project id',
dataset: 'production',
useCdn: false,
// set to
// use current date (YYYY-MM-DD) to target the latest API version }
const client = *createClient*(config);
const response = await client.*fetch*('*[_type == "ignoredHandles"]');
const ignoredHandles: *IgnoredHandle*[] = response.*map*((handle: any) => ({
id: handle.id,
handle: handle.handle,
datereview: handle.datereview
}));
return ignoredHandles;
}
export interface
IgnoredHandle { id: string;
handle: string;
datereview: string;
}
user E
/client'import type { ClientConfig } from 'user E
/client';export async function *getIgnoredHandles*() {
const config:
ClientConfig = { projectId: 'project id',
dataset: 'production',
useCdn: false,
// set to
trueto fetch from edge cache apiVersion: '2023-03-03',
// use current date (YYYY-MM-DD) to target the latest API version }
const client = *createClient*(config);
const response = await client.*fetch*('*[_type == "ignoredHandles"]');
const ignoredHandles: *IgnoredHandle*[] = response.*map*((handle: any) => ({
id: handle.id,
handle: handle.handle,
datereview: handle.datereview
}));
return ignoredHandles;
}
export interface
IgnoredHandle { id: string;
handle: string;
datereview: string;
}
Mar 3, 2023, 11:18 PM
L
Hi
user M
here's the stack traceMar 3, 2023, 11:28 PM
L
defineHttpRequestsrc/http/request.ts:31
./node_modules/@sanity/client/dist/index.browser.js
src/index.browser.ts:14
__webpack_require__
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:856
fn
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:150
Module.<anonymous>
h
ttp3000/static/js/154.chunk.js72
./src/services/sanity.ts
http://localhost:3000/static/js/154.chunk.js:318:30
__webpack_require__
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:856
fn
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:150
__webpack_require__
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:856
fn
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:150
28 | 29 | /** @internal */ 30 | export function defineHttpRequest(envMiddleware: Middlewares): HttpRequest { > 31 | const request = getIt([ 32 | ...envMiddleware, 33 | printWarnings, 34 | jsonRequest(),
./node_modules/@sanity/client/dist/index.browser.js
src/index.browser.ts:14
11 | export * from './types' 12 | 13 | // Set the http client to use for requests, and its environment specific middleware > 14 | const httpRequest = defineHttpRequest(envMiddleware) 15 | /** @public */ 16 | export const requester = httpRequest.defaultRequester 17 |
__webpack_require__
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:856
853 | 854 | __webpack_require__.$Refresh$.init(); 855 | try { > 856 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId)); | ^ 857 | } finally { 858 | __webpack_require__.$Refresh$.cleanup(moduleId); 859 | }
fn
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:150
147 | ); 148 | hotCurrentParents = []; 149 | } > 150 | return __webpack_require__(request); | ^ 151 | }; 152 | var ObjectFactory = function ObjectFactory(name) { 153 | return {
Module.<anonymous>
h
ttp3000/static/js/154.chunk.js72
./src/services/sanity.ts
http://localhost:3000/static/js/154.chunk.js:318:30
__webpack_require__
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:856
853 | 854 | __webpack_require__.$Refresh$.init(); 855 | try { > 856 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId)); | ^ 857 | } finally { 858 | __webpack_require__.$Refresh$.cleanup(moduleId); 859 | }
fn
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:150
147 | ); 148 | hotCurrentParents = []; 149 | } > 150 | return __webpack_require__(request); | ^ 151 | }; 152 | var ObjectFactory = function ObjectFactory(name) { 153 | return {
__webpack_require__
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:856
853 | 854 | __webpack_require__.$Refresh$.init(); 855 | try { > 856 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId)); | ^ 857 | } finally { 858 | __webpack_require__.$Refresh$.cleanup(moduleId); 859 | }
/Users/lhightower/projects/pipeline.ui/webpack/bootstrap:150
147 | ); 148 | hotCurrentParents = []; 149 | } > 150 | return __webpack_require__(request); | ^ 151 | }; 152 | var ObjectFactory = function ObjectFactory(name) { 153 | return {
Mar 3, 2023, 11:28 PM
L
let me check
Mar 3, 2023, 11:29 PM
L
"@sanity/client": "^5.2.2", "@sanity/core": "^2.35.6",
Mar 3, 2023, 11:29 PM
L
and my tsconfig.json:{
"compilerOptions": {
"types": [
"@sanity/client"
],
"target": "es2017",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"allowSyntheticDefaultImports": true,
"noFallthroughCasesInSwitch": true
},
"include": [
"src",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
"compilerOptions": {
"types": [
"@sanity/client"
],
"target": "es2017",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"allowSyntheticDefaultImports": true,
"noFallthroughCasesInSwitch": true
},
"include": [
"src",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
Mar 3, 2023, 11:30 PM
Can you check that you’re getting a response by console logging before you map over it?
Mar 3, 2023, 11:32 PM
L
yep
Mar 3, 2023, 11:32 PM
L
give me one min
Mar 3, 2023, 11:32 PM
L
I'm definitely not getting a response... it doesn't get that far
Mar 3, 2023, 11:35 PM
L
Yes, if I understand what you're saying... the project compiles, and then when I go to the page that calls the function getIgnoredHandles, I get the error
Mar 3, 2023, 11:38 PM
Nothing stands out to me as wrong with your code. Maybe try commenting it out, then reenabling line by line?
Mar 3, 2023, 11:46 PM
L
okay, I'll give it a shot
Mar 3, 2023, 11:46 PM
L
Does this make more sense? 44.chunk.js:22 Uncaught (in promise) ReferenceError: Cannot access 'createClient' before initialization
Mar 3, 2023, 11:51 PM
L
user M
^^Mar 3, 2023, 11:53 PM
Hmm, strange. You’re pretty clearly initializing the client before you make that call.
Mar 3, 2023, 11:54 PM
L
yeah, that's what I was thinking
Mar 3, 2023, 11:54 PM
L
I am sure it's something simple I'm doing wrong
Mar 3, 2023, 11:54 PM
L
I initially got the code from this page:
https://www.sanity.io/docs/js-client#get-client-configuration
https://www.sanity.io/docs/js-client#get-client-configuration
Mar 3, 2023, 11:55 PM
L
in TypeScript section
Mar 3, 2023, 11:55 PM
L
I only have one real difference ... this is what's in the doc:
import {createClient, type ClientConfig} from '@sanity/client'
Mar 3, 2023, 11:56 PM
L
but when I use that, I get an error with the "type" reserved word
Mar 3, 2023, 11:56 PM
L
what version of Typescript should I be using?
Mar 3, 2023, 11:56 PM
L
I'm using "typescript": "^4.9.5",
Mar 3, 2023, 11:57 PM
L
no, it's a separate application and I'm just trying to pull information from sanity.io
Mar 3, 2023, 11:59 PM
Hi Lauren. Was this ever working for you in the past (i.e., on different versions)?
Mar 4, 2023, 12:00 AM
L
no... this is the first time I'm trying to do it
Mar 4, 2023, 12:00 AM
L
sure... i didn't know there was one
Mar 4, 2023, 12:01 AM
L
how do I downgrade?
Mar 4, 2023, 12:01 AM
v5of
@sanity/clientno longer supports ES5, and so I’m curious if that might be the issue when working with a Studio on v2.
Mar 4, 2023, 12:01 AM
L
in other words, what version do I need to target
Mar 4, 2023, 12:01 AM
L
is the studio part the @sanity/core?
Mar 4, 2023, 12:02 AM
You can delete your node_modules and lock file (presumably yarn.lock, on a v2 studio), then edit package.json and change the
@sanity/clientdependency to something like:
"@sanity/client": "^4",. You would then run
yarn.
Mar 4, 2023, 12:03 AM
Yes,
@sanity/coreis one of a few different “parts” that were central to a v2 studio. One of the big changes in v3 was going away from that parts system.
Mar 4, 2023, 12:04 AM
L
"@sanity/core": "^2.35.6",
Mar 4, 2023, 12:04 AM
L
what I'd really like to do is upgrade everything to the latest
Mar 4, 2023, 12:05 AM
That’s certainly the preferred approach, so that’s good to hear. There’s a migration guide as well as this community to help you along.
Mar 4, 2023, 12:06 AM
L
great!!
Mar 4, 2023, 12:06 AM
L
I didn't realize I wasn't on the latest
Mar 4, 2023, 12:07 AM
L
user A
user M
just want to say thank you! I was on a plane last week trying to figure out what I'd done wrong and you guys answered my question so quickly! I'm up and running now and loving Sanity.io Mar 6, 2023, 9:39 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.