fastify / fluent-json-schema

A fluent API to generate JSON schemas
MIT License
495 stars 58 forks source link

ReferenceError: Cannot access 'User' before initialization #234

Closed zfadhli closed 10 months ago

zfadhli commented 10 months ago

Prerequisites

Issue

ReferenceError: Cannot access 'User' before initialization
    at Object.User (/home/kowu/workspace/fastify-mongo/src/app/users/schema.ts:1:1)
    at Object.get [as User] (/home/kowu/workspace/fastify-mongo/src/app/users/schema.ts:2:570)
    at S (/home/kowu/workspace/fastify-mongo/src/app/posts/schema.ts:15:46)
    at Object.<anonymous> (/home/kowu/workspace/fastify-mongo/src/app/posts/schema.ts:46:1)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Object.y (/home/kowu/workspace/fastify-mongo/node_modules/.pnpm/tsx@4.3.0/node_modules/tsx/dist/cjs/index.cjs:1:1151)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1137:19)
    at require (node:internal/modules/helpers:121:18)

Node.js v20.3.0

I have two schema, User and Post. Importing Post schema in User schema causes error. Any idea? repo: https://github.com/zfadhli/fastify-mongo

mcollina commented 10 months ago

You are creating a cyclic dependencies between modules. post -> user -> post.