我在尝试使用'runValidators'选项时遇到问题.我的用户架构有一个必须设置为true的电子邮件字段,但每次将新用户添加到数据库(使用'upsert'选项)并且电子邮件字段为空时它不会抱怨:
var userSchema = new mongoose.Schema({
facebookId: {type: Number, required: true},
activated: {type: Boolean, required: true, default: false},
email: {type: String, required: true}
});
Run Code Online (Sandbox Code Playgroud)
findOneAndUpdate 码:
model.user.user.findOneAndUpdate(
{facebookId: request.params.facebookId},
{
$setOnInsert: {
facebookId: request.params.facebookId,
email: request.payload.email,
}
},
{upsert: true,
new: true,
runValidators: true,
setDefaultsOnInsert: true
}, function (err, user) {
if (err) {
console.log(err);
return reply(boom.badRequest(authError));
}
return reply(user);
});
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么,我只是按照文档:http://mongoosejs.com/docs/validation.html
在文档中说如下:
请注意,在mongoose 4.x中,更新验证程序仅在$ set和$ unset操作上运行.例如,无论number的值如何,以下更新都将成功.
我用$ set替换了$ setOnInsert,但结果相同.
是否有任何方法只需要Mongoose.js中单个集合中的一个(或多个,但不是全部)几列?在我的情况下,我使用Passport并希望我的用户通过我提供的提供商之一注册,或者自己创建.但是,我不想要求用户通过任何一个提供商注册,而是要求他/她希望的任何一个提供商.
这里有一个简单的模式,从在scotch.io护照上的教程(注:这是一个例子,我不打算在我的应用程序使用它,但可能会使用类似的东西):
// app/models/user.js
// load the things we need
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
// define the schema for our user model
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
},
facebook : {
id : String,
token : String,
email : String,
name : String
},
twitter : {
id : String,
token : String,
displayName : String,
username : String
},
google : …Run Code Online (Sandbox Code Playgroud) 我想尝试在Nodejs/Mongoose中确保用户的用户名与密码不同的经典之处.
我认为使用单独的验证功能是好的,但我无法弄清楚如何做到这一点.
到目前为止,我已经使用了Alex Young的记事本教程中的模型代码.他创造了一个password我重复使用的虚拟财产.
我已经得到如下基本验证:
function validatePresenceOf(value) {
return value && value.length;
}
User = new Schema({
'username': {
type: String,
validate: [
validatePresenceOf, 'a username is required',
],
index: { unique: true }
},
});
Run Code Online (Sandbox Code Playgroud)
我如何允许验证器访问其他属性?
首先,这没有帮助.
比方说,我们有一个用户模型:
const schema = new mongoose.Schema({
active: { type: Boolean },
avatar: { type: String }
});
const User = mongoose.model('User', schema);
Run Code Online (Sandbox Code Playgroud)
当我们更新它(设置一个头像):
// This should pass validation
User.update({ _id: id }, { $set: { avatar: 'user1.png' } });
Run Code Online (Sandbox Code Playgroud)
我们希望根据当前(或更改的)active属性值对其进行验证.
active 是 falseactive 是 trueconst schema = new mongoose.Schema({
active: { type: Boolean },
avatar: …Run Code Online (Sandbox Code Playgroud) 考虑以下用于在 Mongoose 中保存时间间隔的模式:
let dateIntervalSchema = new mongoose.Schema({
begin: { type: Date, required: true },
end: { type: Date, required: true }
})
Run Code Online (Sandbox Code Playgroud)
如何确保end始终大于或等于begin使用猫鼬验证?
保存项目时,我正在尝试进行验证。这是我的简化模型:
Sample.add({
isPublished: { type: Types.Boolean, default: false },
thumbnailImage: { type: Types.CloudinaryImage, folder: 'samples/thumbnails' },
});
Sample.schema.pre('validate', function(next) {
if (this.isPublished && !(_.isEmpty(this.thumbnailImage.image))) {
next('Thumbnail Image is required when publishing a sample');
}
else {
next();
}
});
Run Code Online (Sandbox Code Playgroud)
如果Sample模型isPublished设置为,true但thumbnailImage尚未设置,我想提出一个错误。当我输入console.log()这些值时,我分别看到true和false,但是在Keystone Admin中没有出现验证错误。
我浏览了Github上用于Keystone的示例应用程序,Mongoose文档中有很多示例,但是我还没有看到能够处理多个文档路径的示例。
使用2个字段(当前有12个upvotes)的猫鼬自定义验证的示例对我也不起作用。
我究竟做错了什么?我正在使用Mongoose 3.8.35。
mongoose ×6
node.js ×6
validation ×4
mongodb ×3
express ×1
javascript ×1
keystonejs ×1
passport.js ×1