【问题标题】:mysqldatabase is not reflecting post request in Postmanmysql 数据库未反映 Postman 中的发布请求
【发布时间】:2020-11-17 07:30:26
【问题描述】:

我想使用 npm 包 knex 和 mysql 将 myback-end 文件夹与 localhost 上的 mysql 数据库连接。

步骤:

  1. 在后端项目文件夹(包含 knex 和 mysql)中运行 nodemon server.js 文件
  2. 在 Postman 中,我这样做:

打开Postman,选择Post Requst,复制URL:localhost:3000/mypath,复制到body:

{ “电子邮件”:“me@gmail.com”, "密码":"香蕉", “名称”:“测试” }

  1. 点击发送

  2. 当我打开数据库时,运行查询:“SELECT * FROM users;” - 我没有看到任何数据。

你知道会发生什么吗?

这是 server.js 文件中的代码:

const express = require('express');
const db = require('knex') ({
client: 'mysql',
connection: {
 host: '127.0.0.1',
 user: 'root',
 password: 'root',
 database: 'mydatabase'
}

});

const app = express();
app.use(express.json());

const database = {
 users: [
  {
    id: '123',
    name: 'John',
    email: 'john@gmail.com',
    password: 'cookies'
    },
  {
   id: '124',
   name: 'Sally',
   email: 'sally@gmail.com',
   password: 'cake', 
   }
  ]
}

app.get('/',(req,res) => {
res.send(database.users);

})

app.post('/mypath', (req,res) => {
const {email, name, password} = req.body;
db('users').insert({
name: name,
email: email,
password: password
}).then (console.log(req.body))
   res.json(database.users);
})

app.listen(3000, () => {
  console.log('app is running on port 3000');
})

由于某种原因,我收到错误消息:“node:13376 UnhandledPromiseRejectionWarning: Unhandled PromiseRejectionWarning: Unhandled Promise RejectionWarning: Unhandled Promise Rejection Warning: Unhandled PromiseRejectionWarning: Unhandled Promise Rejection: Unhandled Promise RejectionWarning: Unhandled Promise RejectionWarning: Unhandled PromiseRejectionWarning: Unhandled PromiseRejectionWarning:

由于某种原因,我收到错误消息。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch( ). DeprecationWarning:不推荐使用未处理的 Promise 拒绝。”

【问题讨论】:

  • 您能否显示您请求的req.body?你能完整的表达你的快递应用文件吗?
  • 请求正文在 Postman 中:{ "email":"me@gmail.com", "password":"Bananas", "name":"Test" } - 我没有快速应用文件中的请求正文。我想使用 Postman 将 json 数据发布到数据库。由于某种原因,没有数据发布到数据库中。

标签: mysql node.js knex.js


【解决方案1】:

您需要从 express 配置 bodyparser 中间件。 为此,只需进行以下配置:

const express = require('express');
const app = express();

app.use(express.json()); //<-- this is the configuration you need.
//Your db configuration

app.post('/mypath', (req,res) => {
   console.log(req.body) //now your body is defined.
})

激活express.json() 将解析您的请求(如果它们带有'content-type':application/json 标头。

您遇到了rejectionErrorHandler,因为您必须在.then() 函数之后捕获错误。这是一个例子:

exports.createUser = (req, res) => {
    db.get().collection('user').insertOne({ "name": req.body.name, "login": req.body.login, "password": req.body.password }).then((result) => {
        res.set('location', `/users/${result.insertedId}`)
        res.status(201).end()
    }).catch((err) => {
        res.status(500).json({ errors: err.message })
    })
}

【讨论】:

  • 您好!我在 server.js 文件中添加了完整代码。事情不工作。另外,我得到 UnhandledPromiseRejectionWarning。请让我知道发生了什么。谢谢
  • 您收到正文内容了吗?
  • 刚刚使用有关 UnhandledPromiseRejectionWarning 的信息编辑了我的答案
  • 是的,我确实在 Postman 中收到了正文内容,因为请求返回状态为 200。但是,当我在 MySQL 中运行时:“SELECT * FROM users;” - 我没有记录。您提供的示例太高级了。我是参加 Udemy Web 开发课程的新手。你能否提供一些我需要理解的基本概念来解决这个问题 - 只需给出关键词。谢谢!
  • 我发现了问题。它与删除 MySQL 中的权限有关。
猜你喜欢
  • 2021-10-15
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 2018-07-18
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多