如何解决补丁请求中的常量在控制台日志中不断给我未定义的错误?
我正在努力使用 MERN 堆栈实现一个喜欢按钮。我目前有以下补丁请求,它以某种方式不断记录未定义的 Box 请求?
我的补丁请求:
router.patch("/box/:id/likePost",function(req,res){
const boxId = box.findById(req.params.id);
box.findByIdAndUpdate(req.params.id,{likeCount: boxId.likeCount +1})
.then (function () {
res.json("Liked")
console.log(req.params.id,"finished with the then statement") })
.catch(function (err)) {
res.status(422).send("error"));
console.log(error);
});
我的like_box函数
export const LIKE_BOX = "LIKE_BOX";
export function likePost(box){
return {
type: LIKE_BOX,box: box,};
}
我的 handle_submission 函数:
function handleSubmit(event){
console.log(box._id)
event.preventDefault();
async function likePost() {
try{
await patch('/api/games/${box._id}/likePost',box);
props.history.push('/games/${game._id}/likePost');
} catch(error) {
console.log(box._id);
console.log(error,"error from the patch front-end");
}
}
likePost();
我从控制台日志中得到的错误如下:
undefined gameList.js:41
PATCH http://localhost:3000/api/games/undefined/likePost 422 (Unprocessable Entity)
undefind GameList.js:49
Error: Request failed with GameList.js.50
status code 422
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handeLoad (xhr.js:62)
"error from the patch front-end"
对不起,如果代码有一些手动错误,我不得不手动将其输入到代码中。有人可以在这里指出我正确的方向吗?我不确定为什么这个错误不断被抛出?我的预感是补丁请求中的 boxId cont 永远不会返回任何内容......
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。