API request returning status code 404 not found

Kaelix

Im making a fantasy stock app. when making a valid request to the API, the data gets returned as expected. If a stock does not exist, the server returns "Request failed with status code 404". I am trying to handle the error by logging the status to the console but the app does not catch the 404 error and the request stays pending. I cant find a way to handle the error so that it exits the promise to show the user that the stock is invalid.

my backend is as follows

app.get('/api/stocks', (req, res) => {
    const stockName = req.query.stockName
    axios({
        method: 'get',
        url: `https://sandbox.iexapis.com/stable/stock/${stockName}/quote?token=${testSecretKey}`
    })
        .then(response => {
            res.json(response.data);
        }).catch(err => {
        console.log(err.message)
    })
})

my front end

    handleSubmit = async e => {
        e.preventDefault();
        if (this.state.searchTerm !== '') {
            await axios.get("api/stocks", {
                params: {
                    stockName: this.state.searchTerm
                }
            })
                .then((res, req) => {
                    if (res.status === 200) {
                        this.setState({ stock: res.data, searchTerm: '', isStockValid: true });
                        console.log(res.status);
                    } else {
                        console.log(res.status)
                    }
                })
                // .catch(() => { console.log('error 404') });
            
            } else {
                alert('Enter a stock symbol. Example: AAPL for Apple inc.');
        }
    }
Mint

You just need to return the error. Currently you're just console.logging the error and then nothing else, thats why it just stays pending.

app.get('/api/stocks', (req, res) => {
    const stockName = req.query.stockName
    axios({
        method: 'get',
        url: `https://sandbox.iexapis.com/stable/stock/${stockName}/quote?token=${testSecretKey}`
    })
        .then(response => {
            res.json(response.data);
        }).catch(err => {
            console.log(err.message);
            res.send(err);
    })
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

expo publish command is returning 'Request failed with status code 404' error

POST request to /api/notes/addnote endpoint returning 404 Not Found error

Microsoft Graph API status code 404 not found error

WebAPi is not returning correct status code for Value not found

HTTP 404 status code (Not Found) shown as 302

NOT FOUND (status code: 404): Unknown Message

Postman returning not found 404 in api of .net core

Symfony ignore 404 status code for get request

Status code: 404, 'Not Found' on ASP.NET Core 6.0 Web API

SilverStripe Sapphire Functional testing on POST request is always returning 404 status

React and Express iTunes Search API :: Error: Request failed with status code 404

I'm getting a ERROR Request failed with status code 404 AxiosError: Request failed with status code 404

API POST request returns 404 route not found

POST request to /API/login got 404 (not found)

MockMvc returning 404 status

When I send 'GET' request, returning '404 page not found' in Golang

Code stuck when the response of an async request is 404 status code

AJAX returning 404 not found

retrofit is returning 404 not found

Status: 404-Not found

Nuget returned an unexpected status code '404 Not Found' - Package on local drive

Response status code does not indicate success: 404 (Not Found) with owin library

Microsoft Computer Vision API returning 404 Resource Not Found

Paypal REST API returning 404 Not Found only in live mode

api status code for post request that has failed

Firebase Cloud Function Keep return "Request failed with status code 404"

React Native Expo Request failed with status code 404

Axios catch error Request failed with status code 404

React - axios.post error 'Request failed with status code 404'