Error creating collection document (only in Payload Cloud)

default discord avatar
hayskapoy
3 months ago
26

I have deployed a project in Payload cloud and this error occurs when I create a new document in a collection but it only happens in Payload Cloud, it doesn't happen in my local machine 😭 and the stack trace seems to be only referencing payload's code.



[2023-06-19T13:45:51]
[13:45:51] ERROR (payload): TypeError: Cannot read properties of undefined (reading 'tempFilePath')
[2023-06-19T13:45:51]
at /workspace/node_modules/payload/dist/uploads/unlinkTempFiles.js:21:22
[2023-06-19T13:45:51]
at Array.map (<anonymous>)
[2023-06-19T13:45:51]
at mapAsync (/workspace/node_modules/payload/dist/utilities/mapAsync.js:5:28)
[2023-06-19T13:45:51]
at unlinkTempFiles (/workspace/node_modules/payload/dist/uploads/unlinkTempFiles.js:19:39)
[2023-06-19T13:45:51]
at updateByID (/workspace/node_modules/payload/dist/collections/operations/updateByID.js:243:49)
[2023-06-19T13:45:51]
at runMicrotasks (<anonymous>)
[2023-06-19T13:45:51]
at processTicksAndRejections (node:internal/process/task_queues:96:5)
  • default discord avatar
    notchr
    3 months ago

    This is odd - I think one of the Payload team members will have to help with this one. @alessiogr @jacobsfletch @jesschow

  • discord user avatar
    jacobsfletch
    Payload Team
    3 months ago

    Do you have the cloud plugin installed? Your error looks related to be related file uploads, and in cloud this plugin ships uploads off to a CDN for you

  • default discord avatar
    hayskapoy
    3 months ago

    Hello! Yes @jacobsfletch, i guessed so too. I can send my collections config for you to look at if needed.

  • discord user avatar
    jacobsfletch
    Payload Team
    3 months ago

    Yea send me your config if possible

  • default discord avatar
    hayskapoy
    3 months ago
    export default {
      slug: "research-documents",
      admin: {
        useAsTitle: "title",
      },
      upload: {
        staticDir: path.resolve(__dirname, "../../research-documents"),
        mimeTypes: ["text/plain"],
      },
      fields: [
        {
          name: "title",
          type: "text",
          required: true,
        },
        {
          name: "description",
          type: "textarea",
          required: true,
        },
        {
          name: "publishedDate",
          type: "date",
          required: true,
        },
        {
          name: "source",
          type: "text",
        },
        {
          name: "embeddings",
          type: "relationship",
          relationTo: "embeddings",
          admin: { readOnly: true },
        },
      ],
      hooks: {
        beforeChange: [
          (async ({ data, req, operation }) => {
            // skip if this is not an update operation
            // also skip if there is no user (i.e. this is a server request)
            if (operation !== "update" || !req.user) return data
    
            // get embeddings ID
            const id = await getResearchDocsEmbeddings(
              req.user.apiKey,
              data as Required<ResearchDocument>
            )
    
            // update the document with the embeddings ID
            data.embeddings = id
            return data
          }) satisfies BeforeChangeHook<ResearchDocument>,
        ],
        afterChange: [
          (async ({ doc, req, operation }) => {
            // skip if this is not an create operation
            // also skip if there is no user (i.e. this is a server request)
            if (operation !== "create" || !req.user) return doc
    
            // get embeddings ID
            const id = await getResearchDocsEmbeddings(req.user.apiKey, doc)
    
            // update the document with the embeddings ID
            return await req.payload.update({
              collection: "research-documents",
              id: doc.id,
              data: { embeddings: id },
            })
          }) satisfies AfterChangeHook<ResearchDocument>,
        ],
      },
    } satisfies CollectionConfig
  • discord user avatar
    jacobsfletch
    Payload Team
    3 months ago

    What about your top-level Payload config?



    staticDir

    is probably the culprit here



    On Cloud, we do not use local storage for uploads



    You need to install

    payloadcms/plugin-cloud

    so that your media is shipped off to a CDN instead of saving to disk

  • default discord avatar
    hayskapoy
    3 months ago
    export default buildConfig({
      serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
      endpoints: [...payloadEndpoints, ...nextEndpoints],
      cors: ["https://dailypersonas.ai", "https://beta.dailypersonas.ai", "http://localhost:3000"],
      admin: {
        user: Users.slug,
        webpack: config => {
          config.resolve.fallback = { ...config.resolve.fallback, fs: false, util: false }
          config.experiments = { ...config.experiments, asyncWebAssembly: true }
          config.module?.rules?.push({
            test: /\.wasm$/i,
            type: "asset/resource",
            generator: {
              filename: "assets/[hash][ext][query]",
            },
          })
          return config
        },
        components: {
          afterDashboard: [AfterDashboard],
        },
        css: path.resolve(__dirname, "styles.css"),
      },
      globals: [...],
      collections: [...],
      typescript: {
        outputFile: path.resolve(__dirname, "payload-types.ts"),
      },
      graphQL: {
        disable: true,
        schemaOutputFile: path.resolve(__dirname, "generated-schema.graphql"),
      },
      plugins: [
        payloadCloud(),
        seo({
          globals: [...],
          uploadsCollection: "media",
          tabbedUI: true,
        }),
      ],
      rateLimit: {
        window: 15 * 60 * 100, // 15 minutes
        max: 250, // limit each IP to 250 requests per window
      },
    })


    yeah i thought staticDir is only used on my local machine but ignored when deployed to payload cloud.



    should i add an if statement to set staticDir to null/undefined/empty-string when running locally?

  • discord user avatar
    jacobsfletch
    Payload Team
    3 months ago

    Hmm actually I'm seeing it done like this in e-commerce template too:

    https://github.com/payloadcms/template-ecommerce/blob/main/src/collections/Media.ts#LL7C1-L8C1


    So something else might be going on here

  • default discord avatar
    hayskapoy
    3 months ago

    yeah i just followed the example 🥲



    but actually the document will be created, the file is uploaded correctly, it's just that this error shows

  • discord user avatar
    denolfe
    Payload Team
    3 months ago

    @hayskapoy I was able to recreate this by

    saving an existing image that has already been uploaded

    but providing no new image. Not your exact scenario, but I'm looking into it right now

  • default discord avatar
    hayskapoy
    3 months ago

    Cool! Thanks Elliot!

  • discord user avatar
    denolfe
    Payload Team
    3 months ago

    @hayskapoy I'm curious, do you have

    useTempFiles

    enabled under

    upload

    in your payload config?

  • default discord avatar
    hayskapoy
    3 months ago

    No, i initialized payloadCloud without any parameters.



    You can refer to my payload config i sent in this thread.



    It also happens when i create a new “document” of the collection

  • discord user avatar
    denolfe
    Payload Team
    3 months ago

    Hmm, interesting. I ran into this with

    plugin-cloud-storage

    but they use a similar mechanism from Payload's core - we'll see if it fixes both



    It'll be in the next release of Payload, about to merge PR after tests pass

  • default discord avatar
    hayskapoy
    3 months ago

    Nice! I’ll leave my feedback here once i tested it. Thanks!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.