[API Error] TypeError: Cannot read properties of undefined (reading 'options')
Step to reproduce
- Init project
- Setup .env with db_url
- run
npm run db:push - run
npx wrangler dev - Access the sample api at path
localhost:8080/api/post/recent
Images
Are there any errors in the dev tools?
Can you try this in server/index.ts
const appCors = cors({
allowHeaders: ["x-is-superjson", "Content-Type"],
exposeHeaders: ["x-is-superjson"],
allowMethods: ["GET", "POST", "PUT", "DELETE","OPTIONS"],
origin: "http://localhost:3000",
credentials: true,
});
/**
* This is your base API.
* Here, you can handle errors, not-found responses, cors and more.
*
* @see https://jstack.app/docs/backend/app-router
*/
const api = j
.router()
.basePath("/api")
.use(appCors)
.onError(j.defaults.errorHandler);
the CORS above probably fixes the issue, let us know if it worked. Thanks @Shivam-002 for helping. Might be worth adding to the default CORS middleware if multiple people face this issue, what was the use case that surfaced this problem?
still got the error 🤔 as the err stack trace showing I think the issue related to init postgres.
Can you please share your code.
my code is basically the template :D, just init with postgresql option and run wrangler dev to start the api server in local env :)
I have the exact same error. I'm using "wrangler dev" with a neon db and can't create a new post. I can do a GET but the template code doesn't work.
This is working for me now.
I found a section on creating your custom config for CORS in the docs: https://jstack.app/docs/backend/app-router#cors
You can also customize CORS if needed using Hono's built-in CORS middleware. In this case, it's very important to whitelist the x-is-superjson header, as JStack uses this internally for c.superjson() responses:
import { cors } from "hono/cors" cors({ allowHeaders: ["x-is-superjson"], exposeHeaders: ["x-is-superjson"], origin: (origin) => origin, // default: allow any origin credentials: true, // default: allow credentials })
I've followed all the recommendations above, but it's still not working.
X [ERROR] TypeError: Cannot read properties of undefined (reading 'options')
at construct
(file:///C:/Users/.../.../node_modules/src/postgres-js/driver.ts:32:10)
at drizzle (file:///C:/Users/.../.../node_modules/src/postgres-js/driver.ts:113:9)
at null.<anonymous> (file:///C:/Users/.../.../src/server/jstack.ts:22:14)
at middlewareHandler
(file:///C:/Users/.../.../node_modules/jstack/dist/server/index.js:5191:29)
at dispatch (file:///C:/Users/.../.../node_modules/hono/dist/compose.js:22:23)
at null.<anonymous>
(file:///C:/Users/.../.../node_modules/hono/dist/compose.js:22:46)
at middlewareHandler
(file:///C:/Users/.../.../node_modules/jstack/dist/server/index.js:5200:17)
at async dispatch
(file:///C:/Users/.../.../node_modules/hono/dist/compose.js:22:17)
at async bodyParsingMiddleware2
(file:///C:/Users/.../.../node_modules/jstack/dist/server/index.js:5127:3)
at async dispatch
(file:///C:/Users/.../.../node_modules/hono/dist/compose.js:22:17)
Is it because some database thing?