服务器启动时的Mongoose ValidationError

时间:2018-06-17 12:45:42

标签: node.js mongodb express mongoose

我有一个模型如下:

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
});
mongoose.model('User', UserSchema);

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

我使用以下代码连接到db:

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect(process.env.DBCONNECTIONSTR, { promiseLibrary: bluebird }).then(() => console.log('connection successful')).catch(err => console.log(err));

但是在服务器启动后我得到以下验证错误。保存记录时,它完全没有错误:

(node:17708) UnhandledPromiseRejectionWarning: ValidationError: name: Path `name` is required., password: Path `password` is required., email: Path `email` is required.
    at new ValidationError (/Users/bochenlin/dev/data.shojo.live/node_modules/mongoose/lib/error/validation.js:27:11)
    at model.Document.invalidate (/Users/bochenlin/dev/data.shojo.live/node_modules/mongoose/lib/document.js:1755:32)
    at /Users/bochenlin/dev/data.shojo.live/node_modules/mongoose/lib/document.js:1627:17
    at /Users/bochenlin/dev/data.shojo.live/node_modules/mongoose/lib/schematype.js:808:9
    at process._tickCallback (internal/process/next_tick.js:112:11)
    at Function.Module.runMain (module.js:692:11)
    at startup (bootstrap_node.js:194

可能出现什么问题?

0 个答案:

没有答案