1
2
submitted
1 month ago* (last edited 1 month ago)
by
trymeout@lemmy.world
to
c/nodejs@programming.dev
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>
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' ]
}
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' ] }