更新数据库中的现有用户数据 [英] Update existing user data in database

查看:92
本文介绍了更新数据库中的现有用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我已经设法将用户信息保存在数据库中.但是,如果Steam数据库中的信息发生更改,我想在他们登录时更新数据库中的用户信息.所以在我的数据库中也是一样.

Currently, I have managed to the save user information inside my database. However I wanted to update the user information inside my database when they logged in if there is changes of information inside Steam database. So it is the same inside my database.

下面是我的用户架构中的信息示例

Below are example of information inside my User schema

const UserSchema = mongoose.Schema({
  username:{
    type: String,
  },
  profileURL:{
    type: String,
  },
  profileImageURL:{
    type: String,
  },
  steamId:{
    type: String,
  }
});

下面是我的app.js的示例.当用户登录时,我检查了数据库中是否存在用户steamId,我想更新用户信息,例如用户名,profileURL,profileImageURL及其SteamID(如果存在),否则我将在数据库中创建一个新用户.我怎样才能做到这一点?目前,我只是返回完成(空,用户).

Below are example of my app.js. When the user login, I checked if the user steamId exist inside my database, I want to update the user information such as username, profileURL, profileImageURL and its steamID if exist else I create a new user inside my database. How can I achieve this? Currently, I just return done(null, user).

passport.use(new SteamStrategy({
    returnURL: 'http://localhost:3000/auth/steam/return',
    realm: 'http://localhost:3000/',
    apiKey: ''
    }, 
    function (identifier, done){
        var steamId = identifier.match(/\d+$/)[0];
        var profileURL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + 'api_Key' + '&steamids=' + steamId;

        User.findOne({ steamId: steamId}, function(err, user){
            if(user){
                return done(null, user); 
            }
            else{
                request(profileURL, function (error, response, body){
                    if (!error && response.statusCode === 200) 
                    {
                        var data = JSON.parse(body);
                        var profile = data.response.players[0];
                        var user = new User();
                        user.username = profile.personaname;
                        user.profileURL = profile.profileurl;
                        user.profileImageURL = profile.avatarfull;
                        user.steamId = steamId;
                        user.save(function(err){
                            done(err, user);
                        });
                    }
                    else
                    {
                        done(err, null);
                    }
                });
            }
        });
}));

推荐答案

您可以通过启用upsert的更新调用来执行此操作.尝试这样的事情:

You could do this with an upsert-enabled update call. Try something like this:

        request(profileURL, function(err, response, body){
          var data = JSON.parse(body);
          var user = {
          //... construct user object
          }
          
          User.findOneAndUpdate({ steamId: steamId }, user, {upsert: true, new: true, setDefaultsOnInsert: true}, function(err, newUser){
            if (err) return handleError(err);
            done(null, newUser);
          });
        });

这篇关于更新数据库中的现有用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆