我无法将我的评论发布。

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 13:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定