Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mongoose验证器;必填项:true";它不起作用了_Node.js_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

Node.js mongoose验证器;必填项:true";它不起作用了

Node.js mongoose验证器;必填项:true";它不起作用了,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,Mongoose验证器无法验证其“必需:true”的位置 mongoose模式和模型的示例代码 const mongoose = require('mongoose'); const Schema = mongoose.Schema; let chargingLocationSchema = new Schema({ values: { type: Array, required: [true, 'Why no values? Always provide values!'

Mongoose验证器无法验证其“必需:true”的位置

mongoose模式和模型的示例代码

const mongoose = require('mongoose');

const Schema = mongoose.Schema;
let chargingLocationSchema = new Schema({
  values: {
    type: Array,
    required: [true, 'Why no values? Always provide values!']
  },
  chargingHours: {
    type: Number,
    min: 0.1,
    max: [24, 'There is only 24 hours in a day']
  }
}, {
  collection: 'charging_locations'
});

module.exports = mongoose.model('ChargingLocations', chargingLocationSchema);
用于猫鼬验证测试的摩卡测试用例

const expect = require('chai').expect;
const ChargingLocations = require('../chargingLocationsModel');

describe('ChargingLocationsModel', () => {
  it('should be invalid if values is empty', (done) => {
    let cl = new ChargingLocations();

    cl.validate((err) => {
      expect(err.errors.values).to.exist;
      done();
    });
  });

  it('charging hours should be within 24', (done) => {
    let cl = new ChargingLocations({ "chargingHours": 30 });

    cl.validate((err) => {
      expect(err.errors.chargingHours).to.exist;
      done();
    });
  });
});

Mongoose版本:这是因为Mongoose类型数组的默认值为空数组。根据mongoose文档,覆盖定义默认值undefined的。有关详细信息,请参阅。

您是否可能在模式中添加了任何路径验证程序?或者该模式是否高于实际的整个模式用于测试?只是为了能够重现而进行确认。实际整个模式用于测试。默认值:未定义,已解决问题。 mongoose": "^5.1.3
ChargingLocationsModel
    1) should be invalid if values is empty
    √ charging hours should be within 24


  1 passing (35ms)
  1 failing

  1) ChargingLocationsModel
       should be invalid if values is empty:
     Uncaught TypeError: Cannot read property 'errors' of null
      at cl.validate (tests\schema.specs.js:9:18)
      at D:\nodeApp\node_modules\mongoose\lib\document.js:1522:5
      at complete (node_modules\mongoose\lib\document.js:1683:5)
      at p.doValidate.skipSchemaValidators (node_modules\mongoose\lib\document.js:1716:20)
      at D:\nodeApp\node_modules\mongoose\lib\schematype.js:800:11
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)