nuxt / content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
https://content.nuxt.com
MIT License
3.14k stars 628 forks source link

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

Closed Rednas83 closed 2 weeks ago

Rednas83 commented 2 months ago

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\@nuxt+content@2.13.2_ioredis@5.4.1_magicast@0.3.4_nuxt@3.13.0_@parcel+watcher@2.4.1_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\path-meta.js:7:34)
  at Object.transform (node_modules\.pnpm\@nuxt+content@2.13.2_ioredis@5.4.1_magicast@0.3.4_nuxt@3.13.0_@parcel+watcher@2.4.1_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\path-meta.js:26:62)
  at <anonymous> (node_modules\.pnpm\@nuxt+content@2.13.2_ioredis@5.4.1_magicast@0.3.4_nuxt@3.13.0_@parcel+watcher@2.4.1_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\index.js:46:16)
  at transformContent (node_modules\.pnpm\@nuxt+content@2.13.2_ioredis@5.4.1_magicast@0.3.4_nuxt@3.13.0_@parcel+watcher@2.4.1_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\transformers\index.js:40:18)
  at parseContent (node_modules\.pnpm\@nuxt+content@2.13.2_ioredis@5.4.1_magicast@0.3.4_nuxt@3.13.0_@parcel+watcher@2.4.1_@types+no_uey65lmm2h5mkv6hvrbyfgcvue\node_modules\@nuxt\content\dist\runtime\server\storage.js:193:18)
  at <anonymous> (node_modules\.pnpm\@nuxt+content@2.13.2_ioredis@5.4.1_magicast@0.3.4_nuxt@3.13.0_@parcel+watcher@2.4.1_@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 commented 2 months ago

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.