Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
React.JS : make fetch to populate select

I'm trying to make a fetch to populate my select, but I can't use the fetch..

const loadOptions = async (searchQuery, loadedOptions, { page }) => {
    const requestMethod = {
                method: "PUT",
                headers: { 'Content-Type': 'application/json' },
                body: { realmIds: [props.realmScelto] }
            };
    
            const response = await fetch('myapi', requestMethod) // error is here, in requestMethod

            // const response = await axios.post(`myapi?page=${page}&size=20`, { realmIds: [props.realmScelto] }) // this works.. it gives me back 1 value, and it ok but add always the same value that I obtain from api

            // const optionToShow = await response.data
            const optionToShow = await response.json()
 return {
            options: optionToShow,
            hasMore: optionToShow.length >= 1,
            additional: {
                page: searchQuery ? 2 : page + 1,
            },
        };
    };

    const onChange = option => {
        if (typeof props.onChange === 'function') {
            props.onChange(option);
        }
    };

    return (
        <AsyncPaginate
            value={props.value}
            loadOptions={loadOptions}

            onChange={onChange}
            isSearchable={true}
            placeholder="Select"
            additional={{
                page: 0,
            }}
        />
    );

The error that I receive in the requestMethod is:

The argument of type '{method: string; headers: {'Content-Type': string; }; body: {realmIds: any []; }; } 'is not assignable to the parameter of type' RequestInit '. The types of the 'body' property are incompatible. The type '{realmIds: any []; } 'is not assignable to type' BodyInit '. In type '{realmIds: any []; } 'The following properties of type' URLSearchParams' are missing: append, delete, get, getAll and 4 more.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For fetch, the body you pass in is not converted to JSON automatically.

Try

const requestMethod = {
    method: "POST",
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ realmIds: [props.realmScelto] })
};
const response = await fetch(`myapi?page=${page}&size=20`, requestMethod)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda