0
我试图在嵌套架构上使用猫鼬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');
为什么这个代码不会产生对嵌套必填字段验证错误?是否有另一种方法来产生验证错误?我错过了什么吗?
好,如果我的领域是不确定是不是他自己生成验证错误,因为我希望它是需要经过未定义? – grll