从 console.log(req.body) 我得到一个填充对象,但它没有写入数据库

From console.log(req.body) I get a filled object, but it is not written to the database

正在尝试将新数据添加到 table 'recipes' 中。

编辑:所以我一直在尝试, 在我的 index.js 文件中显示 console.log(req.body) 我不知道这是否应该是这样,但是如果我在那里或在 INSERT sql 之后输入 console.log(data),我会得到一个 undefined.

在我的 index.js(快速路由器等)文件中,我记录了 req.body 并得到了一个填充对象,以及我从前端发送的响应 Profile.js

在后端创建配方 index.js__

api.post('/recipe', passport.authenticate('jwt', {
    session: false
  }), (req, res) => {
    const data = {
      name: req.body.recipe_name,
      author: req.body.recipe_author,
      meal: req.body.recipe_meal,
      description: req.body.recipe_description,
      ingredients: req.body.recipe_ingredients,
      preparation: req.body.recipe_preparation
    }
    console.log(req.body)
    db.query('INSERT INTO recipes SET ?', data, (err, results) => {
      if (err) {
        res.status(500)
        return;
      }
      db.query('SELECT * FROM recipes', (err, rows) => {
        if (err) {
          res.status(500)
          return;
        }
        res.json({
          recipes: rows
        });
      })
    })
  });

在前端创建食谱 Profile.js__

handleCreate(){
   const jwt = localStorage.getItem('jwt')
   console.log(jwt)
   axios({
     method: 'post',
     data: {
      name: this.state.name,
      author: this.state.author,
      meal: this.state.meal,
      description: this.state.description,
      ingredients: this.state.ingredients,
      preparation: this.state.preparation
     },
     url: 'http://localhost:4000/api/recipe',
     headers: {
       'Authorization': 'Bearer ' + jwt
     }
   }).then((response) => {
     this.setState({ recipe: response.data });
     console.log(this)
   }).catch((error, res) => {
    if(error.response.status === 401) {
       console.log("Error:", error);
       alert("Failed")
     }
   })
 }

输入字段示例:

<TextField helperText="Enter the name of your recipe" label="name" onChange = {(event) => this.setState({name: event.target.value})}/>
<Button color="secondary" onClick={(event) => this.handleCreate(event)}>Submit Recipe</Button>

问题:没有将其写入数据库 所有相关代码都可以在这里找到:https://gist.github.com/MisterSemaan/07d7cf52b0069d2ea89b29f608c2b976

我想我通过更改::

修复了它

来自::

const data = {
      name: req.body.recipe_name,
      author: req.body.recipe_author,
      meal: req.body.recipe_meal,
      description: req.body.recipe_description,
      ingredients: req.body.recipe_ingredients,
      preparation: req.body.recipe_preparation
    }

收件人::

const data = {
  recipe_name: req.body.name,
  recipe_meal: req.body.meal,
  recipe_author: req.body.author,
  recipe_description: req.body.description,      
  recipe_ingredients: req.body.ingredients,      
  recipe_preparation: req.body.preparation
}

在index.js