Why am I Receiving a TypeError when Trying to Upload a New File to OneDrive using Node.js and How Can I Fix It?

This topic was automatically generated from Slack. You can find the original thread here.

Hello,
I am getting the following error from the one drive upload new file action:

TypeError
Cannot read properties of undefined (reading 'ext')

DETAILS
    at Object.run (file:///tmp/__pdg__/dist/code/a1aafce9d85221e893cc06df06052a0b0b24e152098a9b40b1a3aea7b572f655/code/actions/upload-file/upload-file.mjs:60:56)
    at null.executeComponent (/var/task/launch_worker.js:242:22)
    at MessagePort.messageHandler (/var/task/launch_worker.js:748:28)

Here is the node.js code that creates the file:

import fs from "fs";
import { file } from 'tmp-promise';

// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
  async run({ steps, $ }) {

    // Assuming these variables hold markdown-formatted content
const yaml = steps.formatter.$return_value.yaml;
const title = steps.formatter.$return_value.title;
const summary = steps.formatter.$return_value.summary;
const additionalInfo = steps.formatter.$return_value.additional_info;
const transcript = steps.formatter.$return_value.transcript;

// Combine them into one markdown string, add markdown formatting as needed
const markdownContent = `${yaml}

# ${title}

## Summary
${summary}

## Additional Information
${additionalInfo}

## Transcript
${transcript}`;

function sanitizeFilename(filename) {
  // Define a regex pattern that matches disallowed characters
  const invalidChars = /[:"\/\\|?*]+/g;

  // Replace any sequence of invalid characters with a single dash
  const cleanFilename = filename.replace(invalidChars, '-');

  return cleanFilename;
}

// Define the filename
const markdownFilename = sanitizeFilename(title);

    // Step 3: Write the JSON string to a file
    const { path: tempFilePath, cleanup } = await file({postfix: '.md' });
    //await fs.promises.appendFile(tempFilePath, markdownContent).catch(console.error);
    await fs.promises.writeFile(tempFilePath, Buffer.from(markdownContent));

    // Now, tempFilePath holds the path to the file which you can use elsewhere
    console.log(tempFilePath);

    const results = {
      tempFilePath: tempFilePath,
      title: markdownFilename
    }

    // Return data to use it in future steps
    return results
  },
})

How can I fix this? I just entered the parameters as requested.

can you help?

I see that you’re having an issue with the OneDrive “upload new file” action, and you’ve provided the code for creating the file. It seems like the error is related to the ‘ext’ property being undefined. To help you better, I need to see the code for the OneDrive “upload new file” action, which is causing the error. Can you please provide the code for that action or any relevant information about how you’re using the OneDrive action in your workflow?

he is using the builtin OneDrive “upload new file” action provided by Pipedream.

Here is the code: https://github.com/PipedreamHQ/pipedream/blob/master/components/microsoft_onedrive/actions/upload-file/upload-file.mjs

I see that you’re using the built-in OneDrive “upload new file” action provided by Pipedream. The error you’re encountering is related to the ‘ext’ property being undefined. Based on the code you provided for creating the file, it seems like you’re not setting the file extension when you define the filename. To fix this issue, you can modify the sanitizeFilename function to include the file extension: ```
function sanitizeFilename(filename) { // Define a regex pattern that matches disallowed characters const invalidChars = /[<>:"/\|?*]+/g; // Replace any sequence of invalid characters with a single dash const cleanFilename = filename.replace(invalidChars, ‘-’); // Add the file extension const fileExtension = ‘.md’; return cleanFilename + fileExtension; }

Hope this helps!

Thanks for helping out. Sadly, this did not fix the issue, I’m still getting the same error even though the file and name have .md extensions

Could it be a bug of the onedrive action?

This is the line that is failing in the component: const extension = (await fileTypeFromBuffer(data)).ext;

It’s weird that it’s trying to grab the extension automatically even though a file name is already provided:

    const data = fs.readFileSync(filePath);
    const extension = (await fileTypeFromBuffer(data)).ext;
    const name = !filename.includes(".")
      ? `${filename}.${extension}`
      : filename;

Something for Pipedream to fix I guess.

Unless you want to submit a PR for the fix! :sweat_smile:

Yup… it seems I will have to submit an issue on github at least :sweat_smile: