【发布时间】:2021-07-01 21:11:38
【问题描述】:
我目前正在参加全栈 Web 开发人员训练营课程,并负责使用 CRUD 操作创建我的第一个 MERN Stack 应用程序。
更新单个项目时,我收到以下错误:
TypeError: {(intermediate value)} is not a function
我的代码如下:
- 服务器:carsController.js:
exports.updateOneController = (req, res) => {
Car.findByIdAndUpdate(
req.params.id,
{ new: true }({
$set: {
Model: req.body.Model,
Make: req.body.Make,
Owner: req.body.Owner,
Registration: req.body.registration,
Address: req.body.Address,
previousOwners: req.body.previousOwners,
},
})
);
Car.save()
.then((cars) => res.json(cars))
.catch((err) => res.status(400).json("Error updating the car." + err));
};
- 服务器:carsRouter.js:
router.put("/updateOne/:id", cars.updateOneController);
- 客户端:carEdit.js:
const updateOne = (e) => {
e.preventDefault();
axios
.put(`cars/updateOne/${id}`, {
method: "PUT",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
Model: Model,
Make: Make,
Owner: Owner,
Registration: Registration,
Address: Address,
previousOwners: previousOwners,
}),
})
.then((response) => {
Swal.fire({
imageUrl: "./images/success.gif",
imageWidth: 150,
imageHeight: 150,
imageAlt: "Error",
confirmButtonColor: "#007aff",
width: 400,
title: "SUCCESS!",
});
setCars(response.data);
})
.catch((error) => {
console.log(error);
Swal.fire({
imageUrl: "./images/exclamation.gif",
imageWidth: 150,
imageHeight: 150,
imageAlt: "Error",
confirmButtonColor: "#ff0000",
width: 400,
title: "ERROR!",
text: "User data missing",
}).then(function () {
window.location.reload();
});
});
};
<Button
type="button"
title="Update a Car"
onClick={(e) => updateOne(e)}
>
<FontAwesomeIcon icon={faEdit} />
<span id="editone">x1</span> Update
</Button>
我不确定这是否是 findByIdAndUpdate() 和 save() 之间的冲突?
请在此处查看 GitHub 存储库的链接:https://github.com/ChanBos/MERN-Cars-Database-Application
【问题讨论】:
标签: node.js reactjs mongodb express