0

I am trying to combine my react native and database together by using following code:

const axiosInstance=axios.create({
    baseURL: config.API_URL
 })
try{
    const res =  await axiosInstance.get("/all")
    console.log("res",res)
    return res.data
    }catch(err){
        console.log("error:",err)
    }
    await axios.get('http://localhost:5000/all').then(abc=>console.log(abc.data)).catch(err=>console.log(err))

My API link is: http://localhost:5000/all It should get me the json within the api but it just outputs error :

error: [AxiosError: Network Error]

I have used cors and my backend is:

app.use(express.urlencoded({extended: true}))
app.use(express.json())
app.use(cors((req,callback)=>{
    callback(null,{
        origin:true
    })
}))
app.use('/',router)

inside router:

const getAllUser=async(req,res)=>{
    const data =await userSchema.find()
    return res.json(data)
}
router.get('/all',getAllUser);

I cant seem to figure out where the problem is occouring from.

I tried using cors as cors() it didnt work . I have tried both axiosInstance and normal axios method.

Rose Rawal
  • 109
  • 5

1 Answers1

0

Since your api is not the problem and you mentioned using react native, maybe your question is related to this other SO question React Native fetch() Network Request Failed

Oriun
  • 245
  • 1
  • 7