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

313
Views
How to get data using async await?

How to get the data correctly

import { NextApiHandler } from "next";
import data from "../../../lib/data.json";

const cars: NextApiHandler = (_req, res) => {
  res.status(200).json(data);
};

export default cars;

I tried to use async await for getting data but something went wrong. When I`m trying to print in console.log what is "cars" it returns me function instead of promise.

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

0

You can try this way to get data

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

import Item from '../Item/Item';


const Items = () => {
    const [items, setItems] = useState([]);
     
    useEffect( ()=>{
     fetch('http://localhost:5000/items') 
     .then(res => res.json())
     .then(data => setItems(data));  
    }, [])
    return (
        <div id='items' className='container'>
            <h1 className='item-title'>Items For You</h1>
            <div className="items-container">
            {
                items.map(item => <Item
                key={item._id}
                item={item}
                >
                </Item>)
            }
            </div>
        </div>
    );
};

export default Items;
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!