获取补丁请求将我的数据返回为null

发布于 2025-02-08 20:41:21 字数 1583 浏览 1 评论 0原文

app.patch('/api/todos/:id', async (req, res) => {
    try {
        const data = await pool.query("UPDATE todolist SET task = $1 WHERE id = $2;", [req.body.task, req.params.id])
        res.json(req.body)
    } catch (error) {
        console.error(error.message)
    }
})

我正在尝试提出一个提取补丁请求,但是每次我这样做,而不是从警报窗口中抓住值并将其值存储在我的数据库中,而是返回null或一个空字符串。不确定为什么要这样做,因为它在邮递员中很好地工作。任何建议将不胜感激。

import React from "react";

class UpdateBtn extends React.Component {
    
    render() {


        const updateTodo = (e, alert) => {
            fetch('api/todos/' + e, {
              method: 'PATCH',
              header: {
                'Content-Type': 'application/json',
              },
              body: JSON.stringify({ task: alert })
            })
            .then(res => res.json())
            .catch(error => console.error(error.message))
          }

        const handleUpdate = (e) => {
            const alert = window.prompt("Update Task:")
            if (alert.length === 0) {
                return undefined;
            }
            updateTodo(e.target.id, alert)
            // window.location.reload()
        }

        return (
            <button 
            className="updateBtn"
            id={this.props.id}
            value={this.props.value}
            onClick={handleUpdate}>Update</button>
        )
    }
}

export default UpdateBtn;

app.patch('/api/todos/:id', async (req, res) => {
    try {
        const data = await pool.query("UPDATE todolist SET task = $1 WHERE id = $2;", [req.body.task, req.params.id])
        res.json(req.body)
    } catch (error) {
        console.error(error.message)
    }
})

I am trying to make a fetch PATCH request, but every time I do, instead of grabbing the value from the alert window and storing its value in my database, it returns null, or an empty string. Not sure why it is doing this, because it works perfectly well on Postman. Any advice would be appreciated.

import React from "react";

class UpdateBtn extends React.Component {
    
    render() {


        const updateTodo = (e, alert) => {
            fetch('api/todos/' + e, {
              method: 'PATCH',
              header: {
                'Content-Type': 'application/json',
              },
              body: JSON.stringify({ task: alert })
            })
            .then(res => res.json())
            .catch(error => console.error(error.message))
          }

        const handleUpdate = (e) => {
            const alert = window.prompt("Update Task:")
            if (alert.length === 0) {
                return undefined;
            }
            updateTodo(e.target.id, alert)
            // window.location.reload()
        }

        return (
            <button 
            className="updateBtn"
            id={this.props.id}
            value={this.props.value}
            onClick={handleUpdate}>Update</button>
        )
    }
}

export default UpdateBtn;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文