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

142
Visualizações
Getting results of a MongoDB query to use in a select menu

I'm working on a website that allows you to search for people in a database based on location. Initially, the dropdown menu (select) is populated with provinces that available people are in. I'm trying to use a mongo query to populate that select menu. But when I try to get the values outside the function, it does not work and the select menu turns up empty.

import * as React from "react";
import axios from "axios";
  
const Locations = () => {

  let options = null;

  function axiosTest() {
    // This is a server link i created that runs a query that returns distinct provinces from the database
    const promise = axios.get("/api/v2/people/provinces");
    const dataPromise = promise.then(result => result.data).then(data => {console.log(data);return data;});
    // The console.log() above displays all the objects that are in the query given by the server link in an array
    // e.g. ['British Columbia', 'Alberta', 'Saskatchewan', etc.]
  }
  
  var type = axiosTest();
  console.log(type);  // now it displays it as "undefined"

  if (type) {
    options = type.map((el) => <option key={el}>{el}</option>);
  }

  return (
    <div
      style={{
        padding: "16px",
        margin: "16px",
      }}
    >
      <form>
        <div>
          <select>
            {
              /** This is where we have used our options variable */
              options
              // and the select menu is shown as blank, because it doesn't have any options to fill it with
            }
          </select>
        </div>
      </form>
    </div>
  );
};
  
export default Locations;

Can someone please help me get this to work? Is it something to do with Threads and Concurrency? I'm unfortunately rusty at that.

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

0

Function axiosTest() does not return anything. You should specify change your code so the function would return the result of DB query. You can also change .then() syntax with async/await so your code would become more readable.

const Locations = async () => {

  let options = null;

  function axiosTest() {
    return axios.get("/api/v2/people/provinces");
  }
  
  var type = await axiosTest();
  console.log(type); 

  ...

};
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