2017-07-28 10 views
1

でキーエラーコレクションを複製、私はそれがなく、次のものがまたMongoError:E11000はパスポート

{ [MongoError: E11000 duplicate key error collection: KManV3.users 
index: username_1 dup key: { : null }] 
name: 'MongoError', 
message: 'E11000 duplicate key error collection: KManV3.users index: 
username_1 dup key: { : null }', 
driver: true, 
code: 11000, 
index: 0, 
errmsg: 'E11000 duplicate key error collection: KManV3.users index: 
username_1 dup key: { : null }', 
getOperation: [Function], 
toJSON: [Function], 
toString: [Function] 
} 

DBNAMEを与えるべきであるとして、コレクション内の最初のエントリが発生し、次のuserSchema

var mongoose = require("mongoose"), 
passportLocalMongoose = require("passport-local-mongoose"); 

var userSchema = new mongoose.Schema({ 
email: String, 
password: String, 
userName: String, 
fname: String, 
lname: String, 
userType: Number, 
subscribedThreads: [ 
    { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: "Threads" 
    } 
] 
}); 
// add passport methods 
userSchema.plugin(passportLocalMongoose); 
// export modules to be used by the file requiring it 
module.exports = mongoose.model("Users",userSchema); 

を持っています.users.getIndexes()は:

> db.users.getIndexes() 
[ 
{ 
    "v" : 1, 
    "key" : { 
     "_id" : 1 
    }, 
    "name" : "_id_", 
    "ns" : "KManV3.users" 
}, 
{ 
    "v" : 1, 
    "key" : { 
     "password" : 1 
    }, 
    "name" : "password_1", 
    "ns" : "KManV3.users", 
    "background" : true 
}, 
{ 
    "v" : 1, 
    "unique" : true, 
    "key" : { 
     "username" : 1 
    }, 
    "name" : "username_1", 
    "ns" : "KManV3.users", 
    "background" : true 
} 
] 

明らかに、スキーマのすべてのプロパティが一意に設定されているため、データが完全に異なる場合でもコレクションにデータを追加することはできません。私はそれがパスポートの統合によるものかどうか分からない。 passport-local-mongooseためoptionsを見てみると

+1

最初に質問を確認してください。すべての詳細を追加して何も空のままにしてもエラーが表示されます。だから、私はそれが "duplicate thread"のような重複した値のために引き起こされていないと確信しています –

+0

一意の制約はしたくありませんでしたか? nullを含むANYプロパティ値を別のドキュメントと共有する場合、ドキュメントは一意ではないとみなされます。 – Trent

答えて

1

あなたのコレクションは(存在しない・イン・自分のスキーマ) usernameフィールドに一意のインデックスを持っている理由を説明する

usernameField: specifies the field name that holds the username. Defaults to 'username'.

usernameUnique : specifies if the username field should be enforced to be unique by a mongodb index or not. Defaults to true.

あなたが実際にデータベースに追加の文書では、このフィールドを設定しない場合、MongoDBはnullを使用すると、最初の文書が挿入された後、(また、フィールド値usernamenull付き)以降の文書では、意志E11000エラーが発生します。

だから、最初、usernameにインデックスを削除(ともpassword、私はあなたが一度自分のスキーマ内uniqueとしてそのフィールドをマーク仮定)、および使用するpassport-local-mongooseのための適切なフィールド名を設定します。

userSchema.plugin(passportLocalMongoose, { usernameField : 'userName' }); 

(またはemail、そのフィールドを一意のユーザーIDとして使用する場合)

+0

したがって、スキーマのユーザー名はパスポートから固有のプロパティを取得しますか? –

+0

@ pass99-local-mongooseからの約99人の忍者、はい。説明したように、一意のプロパティを取得するフィールドの名前を変更することができます。 – robertklep

+0

それは理にかなって、パスポート - ローカル - マングースを取り除くことによってそれを固定しました。回避策を見つけるでしょう。 –

関連する問題