猫鼬独特的指数不起作用 [英] Mongoose unique index not working

查看:120
本文介绍了猫鼬独特的指数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望重复的用户名能够在我的网站上注册。所以,我在mongoose模型中放置这样的东西:

I don's want duplicate usernames to be able to sign-up on my website. So, I place something like this in mongoose model:

var userSchema = new mongoose.Schema({
  username: { type: String, index: { unique: true }}, 
  password: String
});

但当我创建新用户时如下所示的控制器,它不会抛出异常并创建副本。

But when I create a new user in the controller like below, it does not throw an exception and creates a duplicate.

mongoose.model('User').create({
    username : email,
    password : password
}, function(err, user) {
    if (err) {
        // WHY DOES IT NOT THROW ERROR AND GET HERE?
    }
});

我已经尝试重启我的应用程序和mongod进程。

I have already tried to restart my application and mongod process.

推荐答案

我终于找到了解决这个问题的方法我还有

I finally found a fix to this issue I was also having

你需要


npm install mongoose-unique-validator

npm install mongoose-unique-validator

这篇关于猫鼬独特的指数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆