0

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?

2 Answers 2

2

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()
        }, [])
Sign up to request clarification or add additional context in comments.

1 Comment

Using the local IP address worked for me. Thanks!
0

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.

9 Comments

thanks for answer. can u give me more detail. what should i do?
https://stackoverflow.com/questions/13104690/nodejs-mongodb-object-id-to-string/34445430 Please check this.
What module do you use to connect with mongodb? mongoose' or mongodb`?
i use mongoose,
` app.get('/',(req,res) =>{ Ads.find({}) .then(data =>{ console.log(data) _id.toString() res.send(data) }).catch(err => { console.log(err) }) }) `
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.