jstack icon indicating copy to clipboard operation
jstack copied to clipboard

[API Error] TypeError: Cannot read properties of undefined (reading 'options')

Open phucledien opened this issue 11 months ago • 9 comments

Step to reproduce

  1. Init project
  2. Setup .env with db_url
  3. run npm run db:push
  4. run npx wrangler dev
  5. Access the sample api at path localhost:8080/api/post/recent

Images

Image

phucledien avatar Feb 06 '25 09:02 phucledien

Are there any errors in the dev tools?

ajiohjesse avatar Feb 07 '25 09:02 ajiohjesse

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);

Shivam-002 avatar Feb 07 '25 13:02 Shivam-002

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?

joschan21 avatar Feb 10 '25 14:02 joschan21

still got the error 🤔 as the err stack trace showing I think the issue related to init postgres.

phucledien avatar Feb 14 '25 00:02 phucledien

Can you please share your code.

Shivam-002 avatar Feb 14 '25 05:02 Shivam-002

my code is basically the template :D, just init with postgresql option and run wrangler dev to start the api server in local env :)

phucledien avatar Feb 15 '25 12:02 phucledien

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.

Image

Spectralgo avatar Feb 27 '25 17:02 Spectralgo

Image

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
})

Spectralgo avatar Feb 27 '25 17:02 Spectralgo

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?

rzkyyds avatar Feb 27 '25 23:02 rzkyyds