首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js ORM2检查字段是否已经存在

Node.js ORM2检查字段是否已经存在
EN

Stack Overflow用户
提问于 2014-01-18 17:10:44
回答 1查看 741关注 0票数 0

检查字段值是否已经存在的最佳方法是什么。

这是我的模型:

代码语言:javascript
复制
// Set global
var moment = require('moment');
var _  = require('lodash');

// Create model
module.exports = function (orm, db) {

    var Profile = db.define('profile', 
        // Field Properties
        {
            username: {type: 'text', required: true, unique: true},
            name: {type: 'text', required: true},
            email: {type: 'text', required: true},
            password: {type: 'text', required: true},
            birthday: {type: 'date', required: true},
            gender: {type: 'enum', values: ["male", "female"], required: true},
            join_date: {type: 'date'}
        },
        {
            // Model hooks. Manual: https://github.com/dresende/node-orm2/wiki/Model-Hooks
            hooks: {

                beforeValidation: function() {

                    // Set join date to current date
                    this.join_date = new Date();
                }
            },

            // Model Validations. Manual: https://github.com/dresende/node-orm2/wiki/Model-Validations
            validations: {
                username: [orm.enforce.security.username({length: 4}, 'Invalid username')],
                email: [orm.enforce.patterns.email('Please enter a valid email')],
                password: [orm.enforce.security.password('6', 'Invalid password')],
                birthday: [orm.enforce.patterns.match(/\d{2}-\d{2}-\d{4}/, null, 'Invalid birthday')]
            },

            // Model methods. Extra functions and stuff
            methods: {

            }
    });
};

这位是我的注册管理员:

代码语言:javascript
复制
module.exports = function (req, res, next) {

    // Get post params
    var params = _.pick(req.body, 'formAction', 'username', 'password', 'email', 'confirm_password',
                            'birthday', 'gender', 'terms');

    // If we try to register
    if (params['formAction'] == 'register') {

        // Manual validations
        // Check if we agreed with the terms
        if (params['terms'] != 1) {

            res.send({error: 'You must agree to the terms of service'});
            return false;   
        }

        // Check if password was confirmed
        if (params['password'] && params['password'] != params['confirm_password']) {

            res.send({error: 'Please confirm your password'});
            return false;
        }

        // Check if username already exists


        // Try to register
        req.models.profile.create({username: params['username'], 
                                   password: params['password'],
                                   email: params['email'],
                                   birthday: params['birthday'], 
                                   gender: params['gender'],
                                   name: params['username']}, function (err, items) {

                                       // Check to see if we have error
                                       error = helpers.getError(err);

                                       // Return error
                                       if (error)
                                           res.send({error: error});
                                   });
    }
    // Show login form
    else
        res.sendfile(settings.path + '/public/register.html');
};

如何检查数据库中是否已经存在用户名?现在,如果我试图创建,则从数据库中获得DUP_KEY错误。

谢谢你,拉杜

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-18 18:18:03

看起来像是添加了一个钩子并使用了next()

代码语言:javascript
复制
beforeCreate: function (next) {

                    obj = this;
                    Profile.exists({email: this.email}, function (err, exists) {
                        if (exists) {
                            return next(new Error("Email already exists"));
                        }
                        else
                        {
                            Profile.exists({username: obj.username}, function (err, exists) {
                                console.log(exists);
                                if (exists) {
                                    return next(new Error("Username already exists"));
                                }
                                else
                                    return next();
                            });
                        }
                    });
                }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21207224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档