I've done a lot of research to find this answer to my problem, but I haven't discovered anything. Feel free to attach a link if I missed the answer.
I did find this solution, but I'm not exactly sure how it might apply to this situation: E11000 duplicate key error index in mongodb mongoose
I'm trying to connect my Express.js, MongoDB, Passport.js app to Mongo-Atlas. I have my Atlas cluster set up and accepting new Users from my localhost and using Google Oauth 2.0, however, when I try to register or log-in using Facebook, I get the following error.
MongoError: E11000 duplicate key error collection: userDB.users index: username_1 dup key: { username: null }
Could it have something to do with this?
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
It worked when connected to my localhost MongoDB, and as I stated I'm having trouble finding a solution.
passport.use(new FacebookStrategy({
clientID: process.env.FBAPPID,
clientSecret: process.env.FBSECRET,
callbackURL: "http://localhost:3000/auth/facebook/secrets"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOrCreate({ facebookId: profile.id }, function (err, user) {
return cb(err, user);
});
}
));
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/secrets',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/secrets');
});
UPDATE
So I've found some documentation about dropping Indexes on MongoDB However that wasn't entirely clear, so I also found an article Dropping Existing Indexes So I implemented the code into my own app.js file.
// Dropping an Index in MongoDB
User.collection.dropIndex({name : "username_1"}, function(err, res) {
if (err) {
console.log('Error in dropping index!', err);
}
});
I've been dropping the Database in between testing to see if that works, but I've been getting thrown this error everything, no matter how I alter the code to try and fix it!
{ _bsontype: 'Timestamp', low_: 1, high_: 1586408833 },
ok: 0,
errmsg: `can't find index with key: { name: "username_1" }`,
code: 27,
codeName: 'IndexNotFound',
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1586408833 },
signature: { hash: [Binary], keyId: [Long] }
},
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {}
}
And yet when I run db.users.getIndexes()
in my Mongo Shell connected to my Atlas server. Sure enough there it is! I even tried using dropIndexes
and it only dropped the email index! I am so frustrated with this!
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "userDB.users"
},
{
"v" : 2,
"key" : {
"email" : 1
},
"name" : "email_1",
"ns" : "userDB.users",
"background" : true
},
{
"v" : 2,
"unique" : true,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "userDB.users",
"background" : true
}
]