Open himanshuclub88 opened 5 years ago
Same with me
when I try to add my second email I got eroor
did your error got fixed
he reason behind this error is that. The index is not present in your collection, in which you are trying insert. So Solution is to drop that collection and run your program again
getspooky How to drop a collection?
db.collectionname.drop()
getspooky How to drop a collection?
I was also facing the same problem. I fixed it by just changing 2 line of code. By adding the email field in :
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/secrets", userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo" }, function(accessToken, refreshToken, profile, cb) { console.log(profile); User.findOrCreate({ username: profile.emails[0].value, googleId: profile.id, }, function (err, user) { return cb(err, user); }); } ));
and also in :
app.get('/auth/google', passport.authenticate('google', { scope: ['profile',"email"] }));
Actually, the package mongoose-findorcreate implicitly passes two arguments(ID and Username) to the mongo server, When you tap into the first user, You store only the "ID" of the user, So the findorcreate package assigns null value to the username field. Again when you try to tap into second or multiple users, Since you already set a null value to the username field. MongoDB server allows only one entry of the null valued field. So It will throw an error.
Here is the solution. Replace the existing package function of findorcreate with the below code
User.findOne( {googleId : profile.id}, function( err, foundUser ){
if( !err ){ //Check for any errors
if( foundUser ){ // Check for if we found any users
return cb( null, foundUser ); //Will return the foundUser
}else { //Create a new User
const newUser = new User({
googleId : profile.id
});
newUser.save( function( err ){
if(!err){
return cb(null, newUser); //return newUser
}
});
}
}else{
console.log( err );
}
});
Finally by adding this code may solve your issue. Also, remove the findorcreate plugin to your userSchema.
That happened to me when i changed a collection's schema which already had documents and tried adding new documents with the new type of schema. The solutions was to delete the collection because it had different types of schema.
you have to add to your schema googled const userSchema = new mongoose.Schema({ email: String, password: String, googleId: String }) drop the collection and try again
i had the same problem and i fixed it by adding a username to my entries, here's my code passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/home" }, function(accessToken, refreshToken, profile, cb) { console.log(profile); User.findOrCreate({ username: profile.displayName, googleId: profile.id }, function (err, user) { return cb(err, user); }); } )); see the username:profile.displayName in findorcreate, now all your entrues have a username so it wont show username=null
I was also facing the same problem. I fixed it by just changing 2 line of code. By adding the email field in :
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/secrets", userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo" }, function(accessToken, refreshToken, profile, cb) { console.log(profile); User.findOrCreate({ username: profile.emails[0].value, googleId: profile.id, }, function (err, user) { return cb(err, user); }); } ));
and also in :
app.get('/auth/google', passport.authenticate('google', { scope: ['profile',"email"] }));
Thanks for the solution, but can you explain or share a link to understand why this works?
I had the same issue, I deleted the respective collection from database and recreated it. Issue resolved.
I added 'email' as an argument in the callback function and it worked fine
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/home" }, function(accessToken, refreshToken, profile, email , cb) { console.log(profile); User.findOrCreate({ username: profile.displayName, googleId: profile.id }, function (err, user) { return cb(err, user); }); } ));
But on later steps you have to remove other changes to store googleId in schema for which one has to edit the schema ny adding a field later :
googleId : String
removing the above changes
I have experienced the same issue. After some googling, I simply drop my "users" collection. Then everything just works fine. I still don't know what was the cause.
he reason behind this error is that. The index is not present in your collection, in which you are trying insert. So Solution is to drop that collection and run your program again
Thank god! This works to me!
That happened to me when i changed a collection's schema which already had documents and tried adding new documents with the new type of schema. The solutions was to delete the collection because it had different types of schema.
I have experienced the same issue. After some googling, I simply drop my "users" collection. Then everything just works fine. I still don't know what was the cause.
Hey, sorry but none of the solutions are working for me
just add another field in the mongoose schema along with googleId. like this. {username: profile.displayName, googleId: profile.id }, and it should work. also try to drop the collection and rerun the application i it doesn't work.
Just modify your findOrCreate inputs like this: function(accessToken, refreshToken, profile, cb) { User.findOrCreate({ username: profile.id, googleId: profile.id }, function (err, user) { return cb(err, user);});} Set username: profile.id since it is unique. I had some issues when used "username: profile.displayName" an diferent social media strategies.
he reason behind this error is that. The index is not present in your collection, in which you are trying insert. So Solution is to drop that collection and run your program again
Still getting Same error.
I figured out that this problem may caused by the indexes which automatically set by the database. You can remove the username_1 index in the database, and eventually this problem will no longer exists. Hope it helps!
getspooky How to drop a collection?
this will work, i was facing the same issue and dropping the collection fixed it
getspooky How to drop a collection?
db.collectionName.drop()
Create a collection using code instead of using client software
when I try to add my second email I got eroor