I am sharing my code please find my issue
BackEndcode
const express = require(“express”);
const cors = require(“cors”);
const mongoose = require(“mongoose”);
const app = express();
app.use(cors());
app.use(express.json());
mongoose
.connect(“mongodb://127.0.0.1:27017/sunday”)
.then(() => {
console.log(“database connected”);
})
.catch(() => {
console.log(“database disconnected”);
});
const frutits = mongoose.model(
“fruiting”,
{
name: String,
},
“monday”
);
app.get(“/fruitlist”, (req, res) => {
frutits.find().then((retdata) => {
res.send(retdata);
});
});
app.post(“/addfruit”, (req, res) => {
var newfruit = req.body.newfruit;
const newFruit = new Fruit({
name: newfruit,
});
newFruit.save().then(() => {
console.log(“logged successfully”);
});
});
app.listen(5000, () => {
console.log(“Server started”);
});
Frontend Code
import { useState } from “react”;
import axios from “axios”;
export default function Todolist() {
const [inputing, inputChange] = useState();
const [fruit, setfruit] = useState();
useState(() => {
axios.get(“http://localhost:5000/fruitlist”).then((data) => {
setfruit(data.data);
});
}, );
function handleChange(evt) {
inputChange(evt.target.value);
}
function add() {
axios.post(“http://localhost:5000/addfruit”, {
newfruit: inputing,
});
setfruit([...fruit, { name: inputing }]);
inputChange("");
}
return (
<button onClick={add}>Submit</button>
{fruit.map((item, index) => {
return <h1 key={index}>{item.name}</h1>;
})}
</div>
</div>
);
}