不正确的整数值:'undefined' 列 'user_id'

Incorrect Integer value: 'undefined' for column 'user_id'

我在这里有点困惑,因为我正在尝试提交 post,但是,它给了我 'user_id' 'undefined,但是如果我 console.log 我看到整数是12. 这两天我一直在尝试很多不同的事情,但都没有成功,我看不到这里的问题(请记住,我是编码的初学者)。

这是路线:

routes.post("/profile/share", (req, res) => {
    let { user_id, body, createdAt, updatedAt } = req.body;
    
    db(`INSERT INTO shares (user_id, body, createdAt, updatedAt) 
        VALUES ('${user_id}', '${body}', '${createdAt}', '${updatedAt}'); 
        `)
        .then(results => {
            if(!results.error) {
                res.status(201).send({})
            }
            res.send(results)
        })
        .catch(err => res.status(500).send(err))
})

这是我组件中的函数:

handleSubmit = event => {
      event.preventDefault()
      const { user_id } = this.props.user[0].id
      console.log(this.props.user[0].id)
      const { body, createdAt, updatedAt } = this.state
          axios.post("http://localhost:7001/api/profile/share", {
            data: {
              user_id,
              body,
              createdAt,
              updatedAt
            }
          }) 
          .then(response => {
            console.log(response.data)
            this.setState(state => ({
              loggedIn: !state.loggedIn,
            }))
          })
          .catch(error => {
            console.log(error)
          })
    }

这是控制台:

错误 500 的消息:

有没有人发现我的错误在哪里?任何帮助将不胜感激。

提前致谢。

根据控制台日志, this.props.user[0].id 是一个数字。你不应该解构它,相反,你可以直接将它分配给一个变量并使用它。

在解构过程中,user_id 未定义导致了问题。

const { user_id } = this.props.user[0].id;  This should be change to

const user_id = this.props.user[0].id;

传递如下参数:-

axios.post("http://localhost:7001/api/profile/share", {
              user_id,
              body,
              createdAt,
              updatedAt
          })