Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR [nitro] [unhandledRejection] Cannot read properties of undefined (reading 'split') #2766

Open
Rednas83 opened this issue Aug 27, 2024 · 1 comment

Comments

@Rednas83
Copy link

Rednas83 commented Aug 27, 2024

Environment



Reproduction

I was not sure where to put the transformer and module so I place it directly inside the root folder. Is that okay?
./plc_transformer.ts

import { defineTransformer } from "@nuxt/content/transformers/utils"
import { readFileSync } from "node:fs"
import { resolve } from "node:path"
import * as cheerio from "cheerio"
import { z } from "zod"

// Define the schema
const schema = z.array(
  z.object({
    id: z.number(),
    comment: z.string().optional(),
    code: z.string(),
  })
)

// Define the transformer
export default defineTransformer({
  name: "plc",
  extensions: ["XSY", "L5X"], // TODO OPENESS XML
  parse(_id: string) {
    const [, name, extension] = _id.match(/^content:(.+)\.(\w+)$/) || [] // TODO Integrate with matchx

    // Load the XML file
    const file = cheerio.load(readFileSync(resolve(`./content/${name}.${extension}`)), { xmlMode: true })

    // Extract and transform the data to an object
    var data: z.infer<typeof schema> = []
    if (extension == "L5X") {
      data = (file("RLLContent Rung") as any)
        .map((id: number, item: string) => {
          return {
            id: id,
            comment: file(item).find("Comment").text().trim() || undefined,
            code: file(item).find("Text").text().trim(),
          }
        })
        .get()
    }

    // Validate the data using Zod
    const zdata = schema.safeParse(data)

    if (zdata.success) {
      return { name: name, type: "plc", body: zdata.data } as any
    } else {
      throw new Error(`Validation failed: ${zdata.error.message}`)
    }
  },
})

./plc_module.ts

import { resolve } from "path"
import { defineNuxtModule } from "@nuxt/kit"

export default defineNuxtModule({
  setup(_options, nuxt) {
    nuxt.options.nitro.externals = nuxt.options.nitro.externals || {}
    nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || []
    nuxt.options.nitro.externals.inline.push(resolve("./plc"))
    // @ts-ignore
    nuxt.hook("content:context", (contentContext) => {
      contentContext.transformers.push(resolve("./content/transformers/plc.ts"))
    })
  },
})

Describe the bug

The transformer is validated as expected, but nitro is returning an error. Any idea how to fix it?

ℹ Vite server warmed up in 4522ms                                                                                                                                    12:22:36
✔ Vite server hmr 107 files in 2128.58ms
[nuxt-app] page:loading:start: 0.03ms
[nuxt-app] app:created: 2.561ms
[nuxt-app] vue:setup: 0.018ms
[nuxt-app] app:rendered: 0.195ms
[nitro-runtime] render:html: 0.683ms
[nitro-runtime] render:response: 0.012ms
[nitro-runtime] beforeResponse: 0.011ms
[nitro-runtime] dev:ssr-logs: 0.007ms
[nitro-runtime] afterResponse: 0.369ms
[nitro-runtime] request: 0.062ms
[nuxt-app] page:loading:start: 0.009ms
[nuxt-app] app:created: 0.756ms
[nuxt-app] vue:setup: 0.013ms
[nuxt-app] app:rendered: 0.046ms
[nitro-runtime] render:html: 0.404ms
[nitro-runtime] render:response: 0.008ms
[nitro-runtime] beforeResponse: 0.007ms
[nitro-runtime] dev:ssr-logs: 0.005ms
[nitro-runtime] afterResponse: 0.299ms
[nitro-runtime] request: 0.044ms
[nitro-runtime] content:file:beforeParse: 0.024ms

 ERROR  [nitro] [unhandledRejection] Cannot read properties of undefined (reading 'split')

  at describeId (node_modules\.pnpm\@[email protected][email protected][email protected][email protected]_@[email protected]_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\path-meta.js:7:34)
  at Object.transform (node_modules\.pnpm\@[email protected][email protected][email protected][email protected]_@[email protected]_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\path-meta.js:26:62)
  at <anonymous> (node_modules\.pnpm\@[email protected][email protected][email protected][email protected]_@[email protected]_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\index.js:46:16)
  at transformContent (node_modules\.pnpm\@[email protected][email protected][email protected][email protected]_@[email protected]_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\index.js:40:18)
  at parseContent (node_modules\.pnpm\@[email protected][email protected][email protected][email protected]_@[email protected]_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\server\storage.js:193:18)
  at <anonymous> (node_modules\.pnpm\@[email protected][email protected][email protected][email protected]_@[email protected]_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\server\storage.js:162:22)

[nitro-runtime] content:file:afterParse: 0.031ms
[nitro-runtime] content:file:beforeParse: 0.023ms
[nitro-runtime] content:file:afterParse: 0.019ms
[nitro-runtime] content:file:beforeParse: 0.033ms
[nitro-runtime] content:file:afterParse: 0.018ms
[nitro-runtime] content:file:beforeParse: 0.015ms
[nitro-runtime] content:file:afterParse: 0.015ms
[nitro-runtime] content:file:beforeParse: 0.021ms
[nitro-runtime] content:file:afterParse: 0.014ms
[nitro-runtime] content:file:beforeParse: 0.024ms
[nitro-runtime] content:file:afterParse: 0.013ms
[nitro-runtime] content:file:beforeParse: 0.024ms
[nitro-runtime] error: 0.047ms
[nitro-runtime] content:file:beforeParse: 0.026ms
[nitro-runtime] content:file:afterParse: 0.022ms
[nitro-runtime] content:file:beforeParse: 0.048ms
[nitro-runtime] content:file:afterParse: 0.074ms
[nitro-runtime] content:file:beforeParse: 0.058ms
[nitro-runtime] content:file:afterParse: 0.022ms

Additional context

@farnabaz
Copy link
Member

Try adding _id to transformer result:

return { _id, name: name, type: "plc", body: zdata.data } as any

Content module uses _id internally to locate and index all contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants