請幫助我解決這個問題,因為我搜索了所有互聯網但找不到答案:(
我正在使用 MERN Stack 制作網站,但遇到了一個問題:
每當我在空的輸入欄位上單擊更新時,我的應用程式就會崩潰。
當我在 Axios 更新請求中捕獲錯誤時,它只是顯示Network Error。
我想問一下有沒有辦法解決這個錯誤?也許如果輸入欄位為空,更新按鈕不會觸發?
這是我的代碼:
// DinnerIdeas.js
import React, { useState, useEffect } from "react"
import './DinnerIdeas.css'
import Axios from "axios"
import FoodListComponent from "../FoodListComponent";
import FormComponent from "../FormComponent";
function DinnerIdeas() {
const [foodName, setFoodName] = useState('')
const [isVegetarian, setVegetarian] = useState(false)
const [priceRange, setPriceRange] = useState('$')
const [newFoodName, setNewFoodName] = useState(null)
const [foodList, setFoodList] = useState([])
// Read:
useEffect(() => {
let unmounted = false
Axios.get("http://localhost:3001/read")
.then((response) => {
if (!unmounted) {
setFoodList(response.data)
}
})
.catch(error => {
console.log(`Hey, the error is ${error}`)
return
})
return () => {
unmounted = true
}
}, [foodList])
// Create:
const addToList = () => {
Axios.post(
"http://localhost:3001/insert",
{
foodName: foodName,
isVegetarian: isVegetarian,
priceRange: priceRange,
}
)
}
// Update:
const updateFood = (id) => {
Axios.put("http://localhost:3001/update", {
id: id,
newFoodName: newFoodName,
})
.catch(error => console.log(`Hey, the error is ${error}`))
}
// Delete:
const deleteFood = (id) => {
Axios.delete(`http://localhost:3001/delete/${id}`)
}
return (
<section className="dinner-ideas">
<FormComponent
setFoodName={setFoodName}
setVegetarian={setVegetarian}
setPriceRange={setPriceRange}
addToList={addToList}
/>
<FoodListComponent
foodList={foodList}
setNewFoodName={setNewFoodName}
updateFood={updateFood}
deleteFood={deleteFood}
/>
</section>
);
}
export default DinnerIdeas;
表單組件:
export default function FormComponent(props) {
return (
<div className="dinner container">
<h1 className="dinner--title">Dinner Ideas</h1>
<form>
<div className="form-group">
<label htmlFor="name">Food name:</label>
<input
type="text"
name="name"
placeholder="Ex: Pesto Pasta"
maxLength="50"
onChange={(event) => {props.setFoodName(event.target.value)}}
required
/>
</div>
<br />
<div className="form-group">
<label htmlFor="vegetarian"> Is this dish Vegetarian?</label>
<select
name="vegetarian"
onChange={(event) => {props.setVegetarian(event.target.value)}}
>
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</div>
<br />
<div className="form-group">
<label htmlFor="price">Price range:</label>
<select
name="price"
onChange={(event) => {props.setPriceRange(event.target.value)}}
>
<option value="$">$</option>
<option value="$$">$$</option>
<option value="$$$">$$$</option>
</select>
</div>
<br />
</form>
<button
type="submit"
className="dinner--btn"
onClick={props.addToList}
>
Add to list
</button>
</div>
)
}
服務器端:index.js
const express = require("express") // Set up an express server
const mongoose = require("mongoose") // Import Mongoose library
const cors = require('cors') // Import CORS to communicate with frontend
const app = express() // Initializing our express server
const DinnerModel = require('./models/Dinner')
app.use(express.json()) // Setting up Middleware
app.use(cors())
// Connect to MongoDB
mongoose.connect(
'mongodb srv://higherstates:<password>@crud.cvewg.mongodb.net/dinner_ideas?retryWrites=true&w=majority',
{
useNewUrlParser: true,
}
)
// Create:
app.post("/insert", async (req, res) => {
const foodName = req.body.foodName
const isVegetarian = req.body.isVegetarian
const priceRange = req.body.priceRange
const dinner = new DinnerModel(
{
foodName: foodName,
isVegetarian: isVegetarian,
priceRange: priceRange,
}
)
try {
await dinner.save()
res.send("data inserted")
} catch(err) {
console.log(err)
}
})
// Read:
app.get("/read", async (req, res) => {
DinnerModel.find({}, (err, result) => {
if (err) {
res.send(err)
}
res.send(result)
})
})
// Update:
app.put("/update", async (req, res) => {
const newFoodName = req.body.newFoodName
const id = req.body.id
try {
await DinnerModel.findById(id, (err, updatedFood) => {
updatedFood.foodName = newFoodName
updatedFood.save()
res.send("update")
}).clone()
} catch(err) {
console.log("The error is: " err)
}
})
app.delete("/delete/:id", async (req, res) => {
const id = req.params.id
await DinnerModel.findByIdAndRemove(id).exec()
res.send("deleted")
})
// Creating a port:
app.listen(3001, () => {
console.log("Server is up on: http://localhost:3001")
})
食物串列組件:
export default function FoodListComponent(props) {
return (
<div className="food-list container">
<h1 className="food-list--title">Food List</h1>
<table>
<thead>
<tr>
<th className="th--name">Name</th>
<th className="th--vegetarian">Vegetarian</th>
<th className="th--price">Price</th>
<th className="th--edit">Edit</th>
<th className="th--actions">Action</th>
</tr>
</thead>
<tbody>
{props.foodList.length > 0 && props.foodList.map((val, key) => {
return (
<tr key={key}>
<td>{val.foodName}</td>
<td>
{val.isVegetarian ? <input type="checkbox" checked readOnly /> : <input type="checkbox" disabled="disabled" readOnly />}
</td>
<td>{val.priceRange}</td>
<td>
<input
name="edit"
placeholder="New food name.."
size="15"
maxLength="50"
onChange={(event) => {props.setNewFoodName(event.target.value)}}
>
</input>
</td>
<td>
<button
className="table--btn"
onClick={() => props.updateFood(val._id)}
>
Update
</button>
<button
className="table--btn"
onClick={() => props.deleteFood(val._id)}
>
?
</button>
</td>
</tr>
)
})}
</tbody>
</table>
</div>
)
}
貓鼬架構:
const mongoose = require('mongoose')
const DinnerSchema = new mongoose.Schema({
foodName: {
type: String,
default: true,
},
isVegetarian: {
type: Boolean,
required: true,
},
priceRange: {
type: String,
required: true,
}
})
const Dinner = mongoose.model("Dinner", DinnerSchema)
module.exports = Dinner
這是演示:
1.如果我點擊**更新按鈕**而不填寫**編輯欄位**,它不會做任何事情,但重繪 頁面時資料會消失。控制臺中的錯誤訊息:
https://i.stack.imgur.com/cyAtQ.gif [Demonstrating gif]
- The app crashed and here's the error message in the server's terminal
https://i.stack.imgur.com/bgayZ.jpg [nodemon app crashed error]
Thanks everyone!
uj5u.com熱心網友回復:
// Update:
const updateFood = (id) => {
if(newFoodName){
Axios.put("http://localhost:3001/update", {
id: id,
newFoodName: newFoodName,
})
.catch(error => console.log(`Hey, the error is ${error}`))
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383867.html
標籤:reactjs mongodb axios schema nodemon
下一篇:更新資料庫后重新渲染
