英文:
I am having trouble getting my comments to post
问题
I am having trouble getting my comments to post. Can someone look at my codes and give me some feedback?
每次我尝试发布评论时,屏幕会闪烁,但没有任何内容被发布。我在Postman上测试了后端功能,一切都正常,所以我认为前端可能有些问题。
我已经尝试了我所能想到的一切。我对编程相对新手,不确定接下来该尝试什么。
useEffect(() => {
if (!loggedIn) {
navigate.push('/login')
}
return () => {
setErrors([])
}
}, [loggedIn, setContent, navigate, setErrors])
const handleSubmit = (e) => {
e.preventDefault();
const newContent = {
content: content,
blog_id: blog.id
}
fetch('/comments', {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(newContent)
})
.then(resp => resp.json())
.then(data => {
if (data.errors) {
setErrors(data.errors)
} else {
addComment(data)
}
});
}
英文:
enter image description hereI am having trouble getting my comments to post. Can someone look at my codes and give me some feedback?
Whenever I post a comment, the screen flashes but nothing posts. I tested the back-end functions on Postman and everything there works fine so I'm assuming something isn't working properly in the front end.
I've tried everything I can. Fairly new to coding and not sure what to try next.
useEffect(() => {
if(!loggedIn) {
navigate.push('/login')
}
return () => {
setErrors([])
}
}, [loggedIn, setContent, navigate, setErrors])
const handleSubmit = (e) => {
e.preventDefaul();
const newContent = {
content: content,
blog_id: blog.id
}
fetch('/comments', {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(newContent)
})
.then(resp => resp.json())
.then(data => {
if(data.errors) {
setErrors(data.errors)
} else {
addComment(data)
}
});
}
答案1
得分: 0
你在handleSubmit
函数声明的正下方拼写错误了preventDefault
,你写成了e.preventDefaul()
。虽然我看不到你组件的其余部分,但我猜测的情况是表单提交后会触发重新加载并阻止 promise 发送。修复这个拼写错误可能会解决你的问题。
英文:
you misstyped preventDefault
just below the function declaration of handleSubmit
you wrote e.preventDefaul()
. While I cannot see the rest of your component what I assume is happening, is that the forms submits which triggers a reload and stops the promise from sending. Fixing this typo will probably fix your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论