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?
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.
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)
})