javascript – 当嵌套文档存在时,如何验证嵌套文档的属性是否存在?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 当嵌套文档存在时,如何验证嵌套文档的属性是否存在?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4682字,纯文字阅读大概需要7分钟。
内容图文
![javascript – 当嵌套文档存在时,如何验证嵌套文档的属性是否存在?](/upload/InfoBanner/zyjiaocheng/773/1868068afa76429f9afb631abf3060da.jpg)
user.schema.js
var Schema = require('mongoose').Schema;
var uniqueValidator = require('mongoose-unique-validator');
var _ = require('lodash');
var userSchema = new Schema({
local: {
username: String, // should exist when local exists
role: String,
hashedPassword: { type: String, select: false }
},
facebook: {
id: String,
token: { type: String, select: false }
},
twitter: {
id: String,
token: { type: String, select: false }
},
google: {
id: String,
token: { type: String, select: false }
}
});
userSchema.path('local').validate(function(local) {
var empty = _.isEmpty(local);
if (empty) {
return true;
}
else if (!empty && local.username) {
return true;
}
else if (!empty && !local.username) {
return false;
}
}, 'Local auth requires a username.');
module.exports = userSchema;
我正在尝试在本地不为空时验证用户名是否存在. IE浏览器.使用本地身份验证时,应该存在用户名.
// should validate
user = {
local: {
username: 'foo';
hashedPassword: 'sfsdfs'
}
};
// shouldn't validate
user = {
local: {
hashedPassword: 'sdfsdfs'
}
};
// should validate (because local isn't being used)
user = {
local: {},
facebook {
...
}
};
我收到此错误:
/Users/azerner/code/mean-starter/server/api/users/user.schema.js:51
userSchema.path('local').validate(function(local) {
^
TypeError: Cannot read property 'validate' of undefined
看来你无法获得物体的路径.我学会了here架构有一个路径属性.当我在console.log(userSchema.paths)时:
{ 'local.username':
{ enumValues: [],
regExp: null,
path: 'local.username',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String] },
_index: null },
'local.role':
{ enumValues: [],
regExp: null,
path: 'local.role',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String] },
_index: null },
'local.hashedPassword':
{ enumValues: [],
regExp: null,
path: 'local.hashedPassword',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String], select: false },
_index: null,
selected: false },
'facebook.id':
{ enumValues: [],
regExp: null,
path: 'facebook.id',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String] },
_index: null },
'facebook.token':
{ enumValues: [],
regExp: null,
path: 'facebook.token',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String], select: false },
_index: null,
selected: false },
'twitter.id':
{ enumValues: [],
regExp: null,
path: 'twitter.id',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String] },
_index: null },
'twitter.token':
{ enumValues: [],
regExp: null,
path: 'twitter.token',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String], select: false },
_index: null,
selected: false },
'google.id':
{ enumValues: [],
regExp: null,
path: 'google.id',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String] },
_index: null },
'google.token':
{ enumValues: [],
regExp: null,
path: 'google.token',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: { type: [Function: String], select: false },
_index: null,
selected: false },
_id:
{ path: '_id',
instance: 'ObjectID',
validators: [],
setters: [ [Function: resetId] ],
getters: [],
options: { type: [Object], auto: true },
_index: null,
defaultValue: [Function: defaultId] } }
所以看起来像local.username和facebook.token这样的路径存在,但不存在像本地和facebook这样的“顶级”路径.
如果我尝试验证local.username路径,它不会像我想要的那样工作.
userSchema.path('local.username').validate(function(username) {
return !!username
}, 'Local auth requires a username.');
仅当local.username存在时才应用验证.我想验证它是否存在.因此,当它不存在时,验证不会被应用,因此它被认为是有效的并被保存.
我也尝试了以下方法,但结果与local.username方法相同(当用户名不存在时,验证不会被点击,并且它被标记为有效).
var Schema = require('mongoose').Schema;
var uniqueValidator = require('mongoose-unique-validator');
var _ = require('lodash');
var userSchema = new Schema({
local: {
username: {
type: String,
validate: [validateUsernameRequired, 'Local auth requires a username.']
},
role: String,
hashedPassword: { type: String, select: false }
},
facebook: {
id: String,
token: { type: String, select: false }
},
twitter: {
id: String,
token: { type: String, select: false }
},
google: {
id: String,
token: { type: String, select: false }
}
});
function validateUsernameRequired(username) {
return !!username;
}
module.exports = userSchema;
解决方法:
Adam,你为什么不尝试一个有条件地将错误传递给下一个函数的预验证钩子.我认为这将为您提供所需的灵活性.如果它不起作用,请告诉我.
例如
schema.pre('validate', function(next) {
if(/*your error case */){ next('validation error text') }
else { next() }
})
这将导致mongoose将ValidationError发送回试图保存文档的任何人.
内容总结
以上是互联网集市为您收集整理的javascript – 当嵌套文档存在时,如何验证嵌套文档的属性是否存在?全部内容,希望文章能够帮你解决javascript – 当嵌套文档存在时,如何验证嵌套文档的属性是否存在?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。