Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
ReactJS promise to array

I'm using react for the frontend, node and mongoDB compass for the backend, I send some information from the database to react, but I keep still getting it as a promise, I need it as an array, but I cannot get it.

Front-end and back-end

const consulta = async () =>{
    let dataRequest = {
      method: 'GET'
    }
    let url = new URL("http://localhost:5000/reservar");
    let response = await fetch(url, dataRequest);
    let result = await response.json();
    return result;
  }

  let promesa = consulta();
  let data_ = promesa.then(function(val) {
    console.log(val);
  });
  console.log(data_);




app.get('/reservar', function(req, res){
    // var userMap = {};
    var userMap = [];
    User.find({estado: 'No reservado'}, function(err, users) {
    
        users.forEach(function(user) {
           userMap.push(user);
          // userMap[user._id] = user;
        });
        res.json(userMap);  
      });
});```    

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In react, if you have an async task like requesting info from a database, you probably want useEffect to change things either. Here is a small example of how it could be done in react.

import React, {useState, useEffect} from "react";

function App(props) {
  const [asyncData, setAsyncData] = useState("loading");
  useEffect(() => {
    (async () => {
      const dataRequest = {
        method: 'GET'
      }
      const url = new URL("http://localhost:5000/reservar");
      const response = await fetch(url, dataRequest);
      const result = await response.json();
      setAsyncData(result);
    })();
  }, []);
  return (
    <div>
      {JSON.stringify(asyncData)}
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!