Row.js component code where i am calling the api key Row.js
JavaScript
x
28
28
1
import React, { useState, useEffect } from 'react';
2
3
import axios from 'axios';
4
5
export default function Row({title, fetchUrl})
6
{
7
const [movies, setMovies] = useState([])
8
9
// A snippet of code that runs based on a specific condition
10
11
useEffect(() => {
12
13
//if [], run once when the row load
14
15
async function fetchData() {
16
const request = await axios.get(fetchUrl);
17
console.log(request);
18
}
19
fetchData();
20
}, []);
21
22
return (
23
<div>
24
<h2>{title}</h2>
25
</div>
26
)
27
}
28
axios.js
JavaScript
1
10
10
1
import axios from "axios";
2
3
// base url to make requests to the movie database
4
5
const instance = axios.create({
6
baseURL: "https://api.themoviedb.org/3",
7
});
8
9
export default instance;
10
Error in the console log is below one, I got the API key from TMBD Api and it was working fine with postman but here in react it is not working properly.
JavaScript
1
17
17
1
Uncaught (in promise) AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}code: "ERR_BAD_REQUEST"config: {transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}message: "Request failed with status code 404"name: "AxiosError"request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}response: {data: '<!DOCTYPE html>n<html lang="en">n<head>n<meta char…not GET /trending/all/week</pre>n</body>n</html>n', status: 404, statusText: 'Not Found', headers: {…}, config: {…}, …}[[Prototype]]: Error
2
fetchData @ Row.js:13
3
await in fetchData (async)
4
(anonymous) @ Row.js:14
5
commitHookEffectListMount @ react-dom.development.js:23150
6
invokePassiveEffectMountInDEV @ react-dom.development.js:25154
7
invokeEffectsInDev @ react-dom.development.js:27351
8
commitDoubleInvokeEffectsInDEV @ react-dom.development.js:27330
9
flushPassiveEffectsImpl @ react-dom.development.js:27056
10
flushPassiveEffects @ react-dom.development.js:26984
11
(anonymous) @ react-dom.development.js:26769
12
workLoop @ scheduler.development.js:266
13
flushWork @ scheduler.development.js:239
14
performWorkUntilDeadline @ scheduler.development.js:533
15
16
17
Advertisement
Answer
you created an axios instance but you did not use it
JavaScript
1
2
1
import instance from "the path to your axios.js file" //like "./axios"
2
and in the fetchData
JavaScript
1
5
1
async function fetchData() {
2
const request = await instance.get(fetchUrl);
3
console.log(request);
4
}
5