[英]mongoose required field nested schema
我正在尝试使用mongoose 4.4.6在嵌套模式中创建必填字段,但从未收到验证错误。这是我的最少非工作代码:
models / Test.js:
var mongoose = require('mongoose');
var TestChildSchema = mongoose.Schema({
_id: false,
testRequiredField: {type: String, required: true}
});
var TestParentSchema = mongoose.Schema({
testField: TestChildSchema
});
module.exports = mongoose.model('Test', TestParentSchema);
我这样使用它:
page.js
var mongoose = require( 'mongoose' );
var Test = mongoose.model('Test');
exports.index = function (req, res) {
var test = new Test();
test.save(function (err, test) {
var strOutput;
res.writeHead(200, {
'Content-Type': 'text/plain'
});
if (err) {
console.log(err);
strOutput = 'Oh dear, we\'ve got an error';
} else {
console.log('test created: ' + test);
strOutput = 'Success';
}
res.write(strOutput);
res.end();
});
和我的app.js :
var http = require('http');
var mongoose = require('mongoose');
var dbURI = 'mongodb://localhost:27017/ConnectionTest';
mongoose.connect(dbURI);
require('./models/Test');
var pages = require('./pages');
http.createServer(function (req, res) {
pages.index(req, res);
}).listen(8888, '127.0.0.1');
为什么此代码未在嵌套的必填字段上生成验证错误? 还有另一种产生验证错误的方法吗? 我想念什么吗?
因为在var test = new Test();
之后,testRequiredField是未定义的var test = new Test();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.