Tag: validation

在node.js数据访问层中的error handling

假设我正在使用一个存储库模式进行数据访问,如下所示: #userController.coffee # `userId` is obtained from the session user = email: 'Bob' password: 'Secret' db.userRepo(@userId).create user, (err, data) => # return results in http response or socket.io 这是在这个方法调用期间可能会出错的地方: 访问数据库时出错 查询中的语法错误 在变更查询期间打破一个约束(我正在使用node-mysql) user有validation错误,例如缺less字段等 用户已经存在user.email 。 我的问题是如何在callback中返回这些错误? callback参数的选项: (err, data) – 其中err是遇到的所有错误的数组。 (err, data) – 其中err是validation错误和数据库错误作为例外抛出。 (err, data) – 除了当user已经存在时,它会返回null,因为这不是一个错误,而是期望的行为。 (err, data, validation) – 其中validation是一个validation错误数组或null 。 […]

有没有一种方法来定义一个没有.save()方法的mongoose模型?

我想要做的是定义一个名为“日程安排”,其中包含一个日程安排实例数组字段的学生模型,但我不希望日程安排模型能够保存到它自己的集合。 这里有一些代码,它会更有意义: var ScheduleSchema = new Schema({ day: {type: Number, min: 0, max: 6}, time: {type: Number, min: 0, max:24} }); var StudentSchema = new Schema({ firstName: String, schedule: [ScheduleSchema] }); var Schedule = mongoose.model("Schedule", ScheduleSchema); var Student = mongoose.model(modelName, StudentSchema); Student.Schedule = Schedule; 我有这个代码的问题是,当我这样做: var schedule = new Student.Schedule({day: 3, time: 15}); 我会得到这样的东西,当我console.log { […]

mongoosevalidation虚拟

我有以下模型使用mongoose: var Usuarios = function (){ var crypt = require('./crypt_helper'), _model, _findByEmail, _findById, _findByRememberToken, _schema, validadores = {} validadores.notBlank = function(val){ return !(/^\s*$/.test(val)); } validadores.email = function(val){ return /^[\w+\-.]+@[az\d\-.]+\.[az]+$/i.test(val); } validadores.unique = function(field, message){ _schema.path(field).validate(function(val, fn){ var query = {} query[field] = val; _model.find(query).exec(function (err, vals) { fn(err || vals.length === 0); }); }, message); […]

关于expressionvalidation的几个问题

我有几个有关节点inputvalidation的问题,也许有一些概念我只是误解,需要澄清。 我目前正在使用express-validator。 我目前有这样的东西: function validate(req) { req.assert('name', 'Please enter a name').notEmpty() req.assert('email', 'Please enter an email').notEmpty() var errors = req.validationErrors(); return errors; }; app.post('/contactform', function(req, res){ var err = validate(req); if (!err) { res.send('everything is good to go') } else { res.send(err) } }); 我理解validation部分,但是一旦我清理了信息,我该如何去实际使用信息呢? 我已经尝试了一些东西 var email = req.sanitize('email').toString() 但这只是给了我一个[object object] 。 所以我有几个问题: 我如何真的去使用消毒的string? […]

请求URL模式,用户信息和path名丢失在heroku上的node.js应用程序

我有一个节点的应用程序,并试图使用url用户信息来授权请求​​。 当我在heroku上运行我的应用程序时,我注意到request.url只包含url的path组件。 有谁知道为什么发生这种情况? 如果有方法可以访问url模式,用户信息和主机名? 这是一个简单的例子: httpServer = http.createServer(function(request, response){ console.log("RECEIVED HTTP REQUEST – " + request.url); }); 当我在本地运行,并发布到http://username:password@localhost:5000/path ,完整的URL被logging: 10:42:03 web.1 | RECIEVED HTTP REQUEST – http://username:password@localhost:5000/path 当我在heroku上运行它,并发布http://username:password@appname.herokuapp.com/path ,它只loggingpath组件: 2014-03-25T15:40:49.965899+00:00 app[web.1]: RECEIVED HTTP REQUEST – /path

jquery ajax基本authentication节点expression

我想通过基本authentication的ajax,将返回401状态 客户 使用coffeescript 使用jquery.base64.js $.ajax url : "http://example.com/" type: "GET" dataType: "text" crossDomain: true beforeSend: (xhr)-> credentials = $.base64.encode("user:pass") xhr.withCredentials = true xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') xhr.setRequestHeader("Authorization", "Basic " + credentials) .always (result, textStatus, jqXHR) => if textStatus == "success" console.log "success" 服务器 使用coffeescript 使用node.js express router.use (req, res, next)-> res.header "Access-Control-Allow-Origin", "http://client.com/" res.header "Access-Control-Allow-Methods", "GET, PUT, […]

链接承诺与mongoose(mPromises)

我正在寻找如何链接使用nodejs / mongoose(mPromise)“fin或创build”function的承诺, 我目前尝试过: var findExistingUsername = function(value){ console.log('existusername'); return mongoose.models["User"].findOne({username: value}) .lean() .exec(); }; var findExistingEmail= function(value){ console.log('existusername'); return mongoose.models["User"].findOne({email: value}) .lean() .exec(); }; var validateEmail = function(value){ console.log('existusername'); var emailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; console.log('Email valido?:' + emailRegex.test(email)); return emailRegex.test(email); }; var addUser = function(data){ console.log('existusername'); return mongoose.models["User"].create(data); }; UserSchema.methods = { validateAndAddUser: function(req, res) […]

在node.js中validation一个对象的接口

我需要在node.js中validation一个对象的接口,所以基本上在我调用这些方法之前检查对象是否有我需要的方法。 我记得有可能使用chriso的validation器来执行这个检查,但我找不到任何有关它的文档的参考。 这是我的记忆失败吗? 而在这种情况下,我该怎么做呢? 编辑为了更好地解释我想要的是一个例子 var myObj = function(parObj){ parObj.hasInterface('methodA', 'methodB', 'methodC') .throwErr(new Error("object must have the right interface")); this.myFun = function(){ return parObj.methodA(/*…*/); }; // … }

手动解密asp.netauthenticationcookie

我在节点js上实现聊天服务器,作为集成到我的aps.net应用程序。 我的用户通过aps.net应用程序进行身份validation,并使用计算机密钥生成身份validationCookie。 不知何故,我需要得到这个身份validationcookie发送到nodejs聊天服务器,并检查cookie是否有效。 到目前为止,我试过这个问题,但没有进展。 PHP开发人员有这个答案 。 在.net应用程序中,它就像这样简单。 但是,现在我正在尝试在nodejs应用程序中使用edgejs ,在nodejs中运行.net代码 我怎样才能手动解密身份validationcookie .net(外部的Web应用程序)?

MongoCRauthentication是否可以使用Compose.io?

我正在尝试使用promised-mongo来访问在Compose.io上托pipe的MongoDB数据库。 我使用相同的连接string来使用mongo CLI工具接受数据库,但是当我尝试连接时,我得到: err { [MongoError: auth failed] name: 'MongoError', message: 'auth failed', ok: 0, errmsg: 'auth failed', code: 18 } 我在promised-mongo代码中找了一下,看起来是使用一个名为MongoCR的标准MongoDB库进行身份validation。 这种身份validation方法是否与Compose.io不兼容,还是必须以某种方式更改数据库的configuration或修改连接string?