node.js - Check if document already exists if not create one -
im learning expressjs + mongo. want check after user logs in passport through steam if data in database if not create record him.
for created static method in schema. unfortunatelly can't save inside of it.
typeerror: object # has no method 'create'
steamaccountschema.statics.checkaccount = function(identifier){ this.findone({ 'identifier' : identifier }, function(err, account){ if(err) throw err; console.log("checking account:" + account) if(account) { console.log("user in db") return true } else { console.log("creating new user account") this.create({ name : 'username', identifier: identifier }, function(err){ if(err) throw err; // if (err) return done(err); return false }); } });
}
just cache this object. i.e. in code below self points need:
steamaccountschema.statics.checkaccount = function(identifier){ var self = this; this.findone({ 'identifier' : identifier }, function(err, account){ if(err) throw err; console.log("checking account:" + account) if(account) { console.log("user in db") return true } else { console.log("creating new user account") self.create({ name : 'username', identifier: identifier }, function(err){ if(err) throw err; // if (err) return done(err); return false }); } }); }
Comments
Post a Comment