1

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

a.v. Magia
  • 43
  • 1
  • 4
  • I'm suspecting that maybe somewhere either in post or in get you must specify that it you send/receive as csv with data:text/csv;charset=utf-8, in http – Costa Jun 14 '21 at 09:31
  • Related answers can also be found [here](https://stackoverflow.com/a/70689003/17865804) and [here](https://stackoverflow.com/a/74810115/17865804) – Chris Jun 11 '23 at 14:50

1 Answers1

0

You should do JSON.stringify before submitting the data. This will convert the Object to string

  • Thanks for your help, although in my case when changing it it keeps giving the same error. I suppose that in addition to what you tell me I must change something else. – a.v. Magia Jun 14 '21 at 09:07