I am trying to create a findOrCreate method in a mongoose model.
UserSchema.statics.findOrCreate = (newUserData, next) ->
@findOne { id: newUserData.id }, (err, user) ->
return next err if err
if not user
self = mongoose.model 'user'
user = new self newUserData
user.save (err) ->
return err if err
next null, user
My main question is: Am I instantiating an new instance of the user model inside a static method correctly?
self = mongoose.model 'user'
user = new self newUserData
I do not see any errors, it ran without any problems. But there is no new document inserted into the database.
did you set the 'safe' option on your schema?