Error Uploading QR Code in Strapi Lifecycle

System Information
  • Strapi Version: 5.1.0
  • Operating System: Windows 11
  • Database: sqlite
  • Node Version: 20.16.0
  • NPM Version: 10.8.1
  • Yarn Version: 1.22.22

I’m experiencing an issue while trying to automatically generate and upload a QR code for a video file in my Strapi application. I have a video collection with the following fields: title (string), video_file (media, single), and qrcode (media, single). The title and video_file fields are required, while the qrcode field is optional.

Here’s the relevant code from my lifecycles.js file:

const QRCode = require("qrcode");

module.exports = {
    async beforeCreate(event) {
        const { data } = event.params;

        try {
            const videoUrl = "http://localhost:1337${data.video_file.url}";

            const qrCodeDataUrl = await QRCode.toDataURL(videoUrl, {
                errorCorrectionLevel: "H",
            });

            const base64Data = qrCodeDataUrl.split(",")[1]; // Get the base64 part
            const buffer = Buffer.from(base64Data, "base64"); // Convert to buffer

            const file = {
                name: "qrcode.png",
                type: "image/png",
                buffer,
            };

            const uploadResponse = await strapi
                .service("plugin::upload.upload")
                .upload({
                    files: [file],
                });

            const qrCodeId = uploadResponse[0].id;

            data.qrcode = qrCodeId;
        } catch (error) {
            console.error("Error generating or uploading QR code:", error);
        }
    },
};

Issue:

I’m encountering the following error:

Error generating or uploading QR code: TypeError: Cannot destructure property 'fileInfo' of 'data' as it is undefined.

Does anyone know what might be causing this issue? Any help or insights would be greatly appreciated!

Thank you!