Reputation: 378
I ran an API on localhost:3000 with express.js and here is the result:
0
_id: "610bcbd9c0ce881847bfd5b6"
__v: 0
1
_id: "610bcc43c0ce881847bfd5b8"
name: "haaa"
title: "data"
title2: "data2"
__v: 0
2
_id: "610bcc5ec0ce881847bfd5ba"
name: "haaa"
title: "data"
title2: "data7"
__v 0
3
_id: "610bcd00303b9a1b041d9d6a"
name: "haaa"
title: "data"
title2: "data7"
__v: 0
when I run axios.get I receive an error.
codes on app.js:
const [data, setData] = useState();
useEffect(() => {
async function fetchmyapi() {
try {
let response = await axios.get('http://localhost:3000/')
setData(response)
console.log(data)
}
catch(error) {
console.log(error)
}
}
fetchmyapi()
}, [])
and here is the error in console.log :
Network Error
at node_modules/axios/lib/core/createError.js:1:0 in <global>
at node_modules/axios/lib/adapters/xhr.js:74:24 in handleAbort
at node_modules/react-native/Libraries/Network/XMLHttpRequest.js:600:10 in setReadyState
at node_modules/react-native/Libraries/Network/XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:189:10 in emit
at [native code]:null in callFunctionReturnFlushedQueue
what is the problem, may i use something else rather than Axios?
Upvotes: 0
Views: 1392
Reputation: 104
You are getting error because _id
When you send data from backend to frontend, you need to convert _id from ObjectID to string.
If you are doing it, there aren't errors.
Upvotes: 0
Reputation: 378
so finally i could figure it out:
1- don't use localhost:3000
as Url, find your IP of network and use it instead like 192.168.1.102
2- Axios
makes problems. use fetch
instead
useEffect(() => {
async function fetchmyapi() {
try {
let response = await fetch('http://yourIP:3000/')
.then((response) => response.json())
setData(response)
}
catch(e) {
console.log(e)
}
}
fetchmyapi()
}, [])
Upvotes: 2