I write this post since I have had the same problem for days and I would like to know if someone has dealt with a similar case before. I have a fronted (nextjs) with which I try to collect a csv and through a fetch pass it to my api, however, when I run the program it gives me an error since it does not get the csv but it literally gets '[object FileList]' . I leave here my fronted and my api in case someone could help me. Thank you very much in advance.
-> Fronted
import { useForm } from 'react-hook-form';
import axios from 'axios';
import { useRouter } from 'next/router';
export default function Home() {
const { register, handleSubmit, errors, reset } = useForm();
const router = useRouter();
async function onSubmitForm(name) {
fetch(`http://127.0.0.1:8000/pruebi/prueba/${Object.values(name)}`, {
method: 'POST',
mode: 'no-cors',
body: name,
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
}
return (
<div>
<form onSubmit={handleSubmit(onSubmitForm)}>
<input {...register("name")} type="file" id="name" name="name" required/>
<button type="submit">Submit</button>
</form>
</div>
);
}
-> Api
from fastapi import APIRouter
from starlette.responses import RedirectResponse
from model.prueba import MongoDB
router = APIRouter()
@router.post("pruebi/prueba/{csv}", name="Insert the date in mongo", tags=["consultas"])
async def insertDate(csv):
db = MongoDB.__init__(dBName="HAR", collectionName="date")
MongoDB.InsertData(db,csv)
The functions init and InsertData are functions that I have in my backend that upload the csv corresponding to mongodb although I do not see relevance for this problem