mongoose-findorcreate writing duplicated users when using passport-facebook mongoose-findorcreate writing duplicated users when using passport-facebook mongoose mongoose

mongoose-findorcreate writing duplicated users when using passport-facebook


The reason why findOrCreate will not work is because findOrCreate query is passing the entire profile object into the query criteria.

If you look here: https://github.com/drudge/mongoose-findorcreate/blob/master/index.js#L22

Depending on what type of data you get back from Facebook, your query will always return empty unless the profile object matches your schema EXACTLY.

What you can do here is this:

User.findOrCreate({ provider: profile.provider, id: profile.id, .... }, (err, user) => {  if (err) {    return done(err);  }  done(null, user);})

instead of profile pass in { provider: profile.provider, id: profile.id, .... }

of course, fix the { provider: profile.provider, id: profile.id, .... } to be matched with your schema definition.


Found the problem - I needed to pass user data to the findOrCreate function in the correct format.

User.findOrCreate( profile._json, (err, user) => {      if (err) {        return done(err);      }      done(null, user);    })