londonappbrewery / Authentication-Secrets

Companion Code for the Authentication Module on The Complete 2019 Web Development Bootcamp
https://www.appbrewery.co
440 stars 525 forks source link

when i try to my second email its showing this error #23

Open being-nitin opened 2 years ago

being-nitin commented 2 years ago

E11000 duplicate key error collection: userDB.users index: username_1 dup key: { username: null }

adithyakb10 commented 8 months ago

This can be fixed by adding a username element to your schema.

const userSchema = new mongoose.Schema({
  username: String,
  name: String,
  googleId: String,
  message: String,
  picture: String,
  messages: Array,
});

Then in findOrCreate add

function (accessToken, refreshToken, profile, cb) {
      // console.log(profile);
      User.findOrCreate(
        {
          googleId: profile.id,
          name: profile.name.givenName,
          picture: profile._json.picture,
          username: profile.displayName,
        },
        function (err, user) {
          return cb(err, user);
        }
      );
    }