【问题标题】:UnhandledPromiseRejectionWarning: ValidationErrorUnhandledPromiseRejectionWarning: ValidationError
【发布时间】:2018-11-05 23:41:08
【问题描述】:

这几天我遇到了麻烦。我正在学习 MEAN 堆栈,但是在使用 mongoose 模式在 mongo 上创建用户期间,我遇到了这个问题:

(node:93337) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: username: Path username is required., password: Path password is required., email: Path email is required.

这是我的代码:

服务器部分:

mongoose.connect('mongodb://localhost:27017/Wisebatt', err => {
  if (err) {
    console.log(`Not connected to db ${err}`)
  } else {
    console.log('Successfully connected to db')
  }
})

...

app.post('/register', (req, res) => {
    const user = new User();
    user.username = req.body.username;
    user.password = req.body.password;
    user.email = req.body.email;
    user.save();
    res.send('User created');
});

用户架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    username: { type: String, required: true, unique: true},
    password: { type: String, required: true },
    email: { type: String, required: true, unique: true},
});

module.exports = mongoose.model('User', UserSchema);

这是我正在使用的附加组件:

  • 快递,
  • Nodemon,
  • 摩根,
  • 正文解析器,
  • Mongo(运行 mongod 和 Mongoose)

【问题讨论】:

  • 你能把你在服务器上设置数据库(mongodb)的部分贴出来吗?我猜你没有正确的凭据或数据库。
  • 当然,这里是:mongoose.connect('mongodb://localhost:27017/Wisebatt', (err) => { if(err){ console.log(`Not connected to db ${err}`); } else { console.log('Successfully connected to db'); } });
  • 您没有设置用户名/密码来使用您当前的 DSN 连接到 MongoDB
  • 你能控制日志 req.body.username 吗?它可能是未定义的。
  • 如果 req.body 未定义,那么问题可能(很可能)出在您未能显示的客户端代码中 - 请显示您的客户端代码

标签: javascript node.js express mongoose mongoose-schema


【解决方案1】:

尝试在您的路线之前将此添加到您的快递代码中。这是一个中间件,当您向后端发送请求时,它将设置 req.body 对象。 (你还需要 npm install --save body-parser)

const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

如果您使用的是 REST 客户端。确保您有一个请求标头,例如:

Content-Type: application/x-www-form-urlencoded

【讨论】:

  • 您能说明您是如何发送请求的吗?通过 HTML?邮差?等等?如果有代码,请发布代码或提及如果使用邮递员。
  • 我怀疑问题可能是由于后端请求的发出方式造成的。
  • 我通过 RESTClient 发送它因为我使用的是 Firefox,但上面没有邮递员 这是正文请求:{ 'username': 'StringData', 'password': 'SomePassword', 'email': 'random@mail.com' }
  • 如果您使用的是 REST 客户端。确保您有这样的请求标头。 Content-Type 设置为 application/x-www-form-urlencoded
  • app.use(bodyParser.urlencoded({ extended: true }));是这个吗?
【解决方案2】:

好的,我找到了问题...

显然,问题是由于以下两者之一:

  • 使用的浏览器,
  • 发送 POST 请求的扩展

惊喜,我用 Postman 试过了,请求成功了。所以所有代码都很棒,问题出在两者之一。

所以,这让我学到了一件事。如果不是你的代码,是你使用的软件可以摧毁你所做的一切

【讨论】:

  • 您最终是否像我提到的那样使用 Content-Type: application/x-www-form-urlencoded?因为如果是这样,获得赞成或标记为答案会很好。您在这里的回答并没有真正向下一个遇到此问题的用户提供任何信息
  • 是的,我添加了它,我会投票,谢谢你的帮助:)
【解决方案3】:

如果您使用的是 expressJS,请确保您必须添加此行。

const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

特别是这个,

app.use(express.urlencoded({ extended: true }));

这实际上解决了我的问题。

【讨论】:

    【解决方案4】:

    用途:

    app.use(express.urlencoded({ extended: true })); // this is for app.post to submit route.. 
    

    app.jsindex.js

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2019-12-18
      • 1970-01-01
      • 2013-07-16
      • 2015-02-15
      • 2020-01-02
      • 2020-06-19
      • 2019-03-16
      • 2021-08-07
      相关资源
      最近更新 更多