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

1.1K
Views
Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource

So this is my api code, I am trying to request this api through script.js on my frontend, but I get an error.

var express = require("express");
var app = express();
const cors = require('cors')
port = 8080

app.use(cors(
    {
        origin: "*",
    }
))


app.get('/tshirt', (req, res) =>{
    res.status(200).send({
        tshirt: '๐Ÿ‘•',
        size: 'large'
    })
})

app.post('/tshirt/:id', (req, res) => {
    
    const { id } = req.params;
    const { logo } = req.body;

    if (!logo) {
        res.status(418).send({message: 'We need a logo!'})
    }
    
    res.send({
        tshirt: `๐Ÿ‘• with your ${logo} and ID of ${id}`,
    })

})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
  })

and this is the fetch request in script.js

const api_url = 'localhost:8080/tshirt'
async function getTshirt() {
    const response = await fetch (api_url);
    const data = await response.json();
    const { tshirt, size } = data;
    console.log(size)
    console.log(tshirt)
}

getTshirt()

But then I get this error

Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.
    getTshirt http://127.0.0.1:5500/script.js:3
    <anonymous> http://127.0.0.1:5500/script.js:10

I have tried googling this and looking all over the site but no solutions work. Please help.

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

You have to catch any possible promise rejections because if it rejects and you don't have a handler for it, then you will get this error and you won't have done anything intelligible with the error:

getTshirt().catch(err => {
    console.log(err);
    // do something with the error here
});

In the example, you show above, it appears you are getting some sort of NetworkError when trying to access your api_url. So, you'll have to look into the reasons for that error and attempt to fix why it's getting that error in the first place. But, even still, you should always be catching promise rejections.

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!