简体   繁体   English

如果没有创建文档,请检查文档是否已存在

[英]Check if document already exists if not create one

Im learning expressjs + mongo. 我正在学习expressjs + mongo。 I want to check after user logs in with passport through Steam if his data is already in database if not to create a record for him. 我想在用户使用护照通过Steam登录后检查,如果他的数据已经在数据库中,如果不是为他创建记录。

For this I created a static method in schema. 为此我在schema中创建了一个静态方法。 Unfortunatelly i can't save from the inside of it. 不幸的是,我无法从内部拯救。

TypeError: Object # has no method 'create' TypeError:Object#没有方法'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 already 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 the this object. 只需缓存对象即可。 Ie in the code below self points to what you need: 即在下面的代码中, self指向您需要的内容:

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 already 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
            });
        }
    }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查文档是否已经存在,如果是,则进行更新,否则创建新的-猫鼬 - Check if document already exists and if yes then update, otherwise create new - Mongoose 检查之前是否已经存在字段以在 MongoDB / NodeJS 上创建文档 - check if already exists field before to create a document on MongoDB / NodeJS 如何检查具有某些字段的文档是否已存在于 MongoDB 数据库中? - How to check if document with certain fields already exists in the MongoDB database? 如何从猫鼬时间戳检查文档是否已存在? - How to check from Mongoose timestamps if the document already exists? 检查字段是否存在,将其删除并创建一个新字段 - Check if field exists, delete it and create a new one 仅在尚不存在的情况下插入文档 - Insert document only if not already exists 如果文档的 _id 已经存在,如何将数组元素推送到数组,如果 _id 不存在,如何创建新文档? - How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist? Express:查询以检查文档是否存在 - Express: Query to check if the document exists Mongoose 如果不存在则创建多个文档 - Mongoose create multiple document if not exists 更新mongodb文档,如果文档存在,否则创建 - Update mongodb document, if the document exists, else create
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM