猫鼬会忽略发布请求中的字符串属性

时间:2018-11-29 13:36:04

标签: node.js mongodb express curl mongoose

我目前正在使用TypeScript和MongoDB使用Express开发RESTful API。我选择的ORM是猫鼬。使用以下POST请求测试我的应用程序时:

curl -i -X POST -H "Content-Type: application/json" -d '{ "first_name":"hansi"}' localhost:3000

我收到以下错误消息,指出属性“ first_name”是必需的:

{ ValidationError: Person validation failed: first_name: Enter a first name
    at ValidationError.inspect (/Users/schnabl/s6/node_modules/mongoose/lib/error/validation.js:59:24)
    at formatValue (internal/util/inspect.js:453:31)
    at inspect (internal/util/inspect.js:193:10)
    at Object.formatWithOptions (util.js:85:12)
    at Console.(anonymous function) (console.js:188:15)
    at Console.log (console.js:199:31)
    at /Users/schnabl/s6/src/routes/person.js:23:29
    at /Users/schnabl/s6/node_modules/mongoose/lib/model.js:4533:16
    at parallel (/Users/schnabl/s6/node_modules/mongoose/lib/model.js:2667:16)
    at /Users/schnabl/s6/node_modules/async/internal/parallel.js:39:9
    at /Users/schnabl/s6/node_modules/async/internal/once.js:12:16
    at iteratorCallback (/Users/schnabl/s6/node_modules/async/eachOf.js:60:13)
    at /Users/schnabl/s6/node_modules/async/internal/onlyOnce.js:12:16
    at /Users/schnabl/s6/node_modules/async/internal/parallel.js:36:13
    at callbackWrapper (/Users/schnabl/s6/node_modules/mongoose/lib/model.js:2640:20)
    at /Users/schnabl/s6/node_modules/mongoose/lib/model.js:4533:16
  errors:
   { first_name:
      { ValidatorError: Enter a first name
          at new ValidatorError (/Users/schnabl/s6/node_modules/mongoose/lib/error/validator.js:29:11)
          at validate (/Users/schnabl/s6/node_modules/mongoose/lib/schematype.js:871:13)
          at /Users/schnabl/s6/node_modules/mongoose/lib/schematype.js:924:11
          at Array.forEach (<anonymous>)
          at SchemaString.SchemaType.doValidate (/Users/schnabl/s6/node_modules/mongoose/lib/schematype.js:880:19)
          at /Users/schnabl/s6/node_modules/mongoose/lib/document.js:1913:9
          at process._tickCallback (internal/process/next_tick.js:61:11)
        message: 'Enter a first name',
        name: 'ValidatorError',
        properties: [Object],
        kind: 'required',
        path: 'first_name',
        value: undefined,
        reason: undefined,
        [Symbol(mongoose:validatorError)]: true } },
  _message: 'Person validation failed',
  name: 'ValidationError' }

但是,当我注释掉'“ required:”输入一个字符串“”时,不会引发服务器错误。但是我在架构中使用的first_name和所有其他字符串属性仍然没有添加到集合中。

集合中只有Mongo生成的“ ids”和birth_date(current_timestamp)的默认值。

我的架构(personSchema.ts):

//imports

const Schema = mongoose.Schema;

export const PersonSchema = new Schema({
    first_name: {
        type: String
        //required: "Enter a first name"
    },
    ...
    birth_date: {
        type: Date,
        default: Date.now
    } 
}, { collection: 'person' });

路由器(personRouter.ts):

//imports

export class PersonRoute {
    public personController: PersonController = new PersonController();

    public routes(app): void {
      ...
      //getrequests
      ...

      app.route("/").post((req: Request, res: Response) => {

        const PersonModel = mongoose.model('Person', PersonSchema);

        PersonModel.create(req.body, function (err, post) {
          if (err) console.log(err);
          res.json(post);
        });
      });
    }
}

我的测试请求是否可能有问题?

编辑: 我在Postman中测试了发帖请求,仍然得到了相同的结果:

enter image description here

0 个答案:

没有答案