'when I fetch data from server side, I got an error which is...ManageInventory.js:7 Uncaught (in promise) TypeError: res.json is not a function

Here is my index.js from server

const express = require('express');
const cors = require('cors');
const items = require('./data.json');
const port = process.env.PORT || 4000;
const app = express();
app.use(cors())
app.use(express.json())
app.get('/', (req, res) => {
    res.send('Hello....Assalamualaikum')
})
app.get('/items', (req, res) => {
    res.send(items);
})
app.listen(port, () => {
    console.log('listening from', port)
})

Here is ManageInventory.js from front end

import axios from 'axios';
import React, { useState } from 'react';
const ManageInventory = () => {
const [items, setItems] = useState([])
axios({
        method: 'get',
        url: 'http://localhost:4000/items'
    })
        .then(res => res.json())
        .then(data => console.log(data))
    return (
        <>
            <h1>This is manage Inventory page</h1>
        </>
    );
};

export default ManageInventory;

why show the res.json() is not a function?



Solution 1:[1]

write res.data not res.json() Axios takes care of parsing the response automatically for us, so we don't have to call any additional methods.

Solution 2:[2]

Whwn i'am call axios like below then my problem is solved. Thank you all for your kind helpful information.

axios.get('http://localhost:4000/items')
            .then((response) => {
                console.log(response.data)
            })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohnit Shriyan
Solution 2 Md Foyzur Rahman Siam