猫鼬 +每个人都有奇怪的行为

发布于 2024-12-01 02:49:20 字数 1275 浏览 7 评论 0原文

使用 everyauth,当我在网站上使用从未经过身份验证的用户登录时,将启动以下代码。检查用户是否已经在我的 mongodb 中,如果没有,则写入它。现在的问题是,它适用于第一个经过身份验证的用户,然后如果第二个用户登录,我的 joinUser._id 参数会正确启动,但我的架构的所有其他参数都来自第一个保存的用户,就像我的参数一样从未重新初始化...奇怪的是,我的 console.log 显示了正确的新参数,而不是数据库中实际写入的参数。我的身份验证是通过 everyauth 模块完成的,我使用express 框架。

exports.findOrCreateFacebookUser = function(fbUserData, promise){

  User.findOne({_id:fbUserData.id}, function(err, user) {
    if(err) {
      console.log("Error in finding user for auth. Check Db");
      promise.fail(err);
      return;
    }
    else if(user){
      console.log("User found ");
      promise.fulfill(user);     
    }
    else{

      var joiningUser = new User();
      joiningUser._id = fbUserData.id;
      joiningUser.firstName = fbUserData.first_name;
      joiningUser.lastName = fbUserData.last_name;
      joiningUser.email = fbUserData.email;
      //console.log(JSON.stringify(joiningUser));
      joiningUser.save(function(err){
        if(err){
          console.log("Couldnt save new user: " + err);
          promise.fail(err);
          return;
        }
        else{
          console.log("User wasnt existant, it is now created: " + JSON.stringify(joiningUser));
          promise.fulfill(joiningUser);
        } 
      });
    }
  });
};

Using everyauth, when I login with a never autenticated before user on my website, the following code is launched. Checks if the user is already in my mongodb, if not it writes it. The issue right now is that works for the first authenticated user, then if a second user logins, my joiningUser._id parameters is properly initiated, but all the other parameters of my schema are from the first ever saved user, its like my parameters are never reinitialised... weird, my console.log shows the right new parameters, and not what is actually written in the database. My authentication is done via the everyauth module, I use the express framework.

exports.findOrCreateFacebookUser = function(fbUserData, promise){

  User.findOne({_id:fbUserData.id}, function(err, user) {
    if(err) {
      console.log("Error in finding user for auth. Check Db");
      promise.fail(err);
      return;
    }
    else if(user){
      console.log("User found ");
      promise.fulfill(user);     
    }
    else{

      var joiningUser = new User();
      joiningUser._id = fbUserData.id;
      joiningUser.firstName = fbUserData.first_name;
      joiningUser.lastName = fbUserData.last_name;
      joiningUser.email = fbUserData.email;
      //console.log(JSON.stringify(joiningUser));
      joiningUser.save(function(err){
        if(err){
          console.log("Couldnt save new user: " + err);
          promise.fail(err);
          return;
        }
        else{
          console.log("User wasnt existant, it is now created: " + JSON.stringify(joiningUser));
          promise.fulfill(joiningUser);
        } 
      });
    }
  });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-12-08 02:49:20

使用 mongoose-auth。它会为您处理 mongoose 和 everyauth 的组合。

Use mongoose-auth. It takes care of the combination of mongoose and everyauth for you.

み格子的夏天 2024-12-08 02:49:20

为 _id 分配强制值似乎存在问题。因此,我为个人资料特定 id 创建了另一个 id(例如:facebookId)。

There seems to be an issue with assigning _id with a forced value. So I created another id for the profile specific id (ex: facebookId).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文