Can't fetch my data from my backend(NodeJS) to my react native app

151 Views Asked by At

I have tried to get my json data but it's not working. It works to get the data on postman and on chrome but when I try to fetch the data it comes back as null. I'm using apisauce.

any ideas? I'm new to react native :)


const [listings, setListings] = useState([])


useEffect(() => {
        loadListings()
       
    }, []);


const loadListings = async () => {
   const response = await listingsApi.getListings()
   console.log(response.data, "response.data")

   setListings(response.data)


    }

import { create } from "apisauce";

const apiClient = create({
  baseURL: "http://127.0.0.1:9000/api",
});

export default apiClient;

import client from "./client";

const endpoint = "/listings";
const getListings = () => client.get(endpoint);

export default {
  getListings,
};
1

There are 1 best solutions below

1
On

You can call loadListing like that (because loadListing is asynchronous function)

useEffect(() => {
loadListings().then(() => {}).catch(() => {}) 
},[])

or can be called using IIFE

useEffect(() => {
(async() => {
   await loadListings();
})()
},[])