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

TypeError: String(file) on version 2.x #889

Closed
nagayev opened this issue Oct 4, 2022 · 1 comment
Closed

TypeError: String(file) on version 2.x #889

nagayev opened this issue Oct 4, 2022 · 1 comment
Labels

Comments

@nagayev
Copy link
Collaborator

nagayev commented Oct 4, 2022

Support plan

  • Which support plan is this issue covered by? Community
  • Currently blocking your project/work? (yes/no): no
  • Affecting a production system? (yes/no): yes

Context

  • Node.js version: 16.15.1
  • Release Line of Formidable (Legacy, Current, Next): Current
  • Formidable exact version: 2.0.1
  • Environment (node, browser, native, OS): nodejs on linux
  • Used with (popular names of modules): nextjs

What are you trying to achieve or the steps to reproduce?

You can use this code.

import formidable from "formidable";
import fs from "fs";

export const config = {
  api: {
    bodyParser: false
  }
};

const post = async (req, res) => {
  console.log('handling file...');
  const form = new formidable.IncomingForm();
  form.parse(req, async function (err, fields, files) {
    if (err) console.error(err);
    console.log(files.file.newFilename);
    try{
      await saveFile1(files.file);
    }
    catch(e){
      console.log('Error was catched: ',e);
    }
    return res.status(201).send("Nothing");
  });
};

const saveFile1 = async (file) => {
  console.log(file); //**TypeError**
  const data = fs.readFileSync(file.filepath);
  fs.writeFileSync(`./public/${file.originalFilename}`, data);
  //await fs.unlinkSync(file.path);
  return;
};

export default (req, res) => {
  req.method === "POST"
    ? post(req, res)
    : req.method === "PUT"
    ? console.log("PUT")
    : req.method === "DELETE"
    ? console.log("DELETE")
    : req.method === "GET"
    ? console.log("GET")
    : res.status(404).send("");
};
 

What was the result you got?

TypeError: Cannot read properties of undefined (reading 'newFilename')

What result did you expect?

Working app.

@nagayev nagayev added the bug label Oct 4, 2022
@nagayev
Copy link
Collaborator Author

nagayev commented Oct 4, 2022

Workaround:
Apply patch from my PR #890

@GrosSacASac GrosSacASac changed the title TypeError: TypeError: String(file) on version 2.x Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants