如何删除 Meteor 中的登录用户

How to remove logged in user in Meteor

我正在 Meteor 中开发一个应用程序,我想知道如何删除登录到系统的用户帐户?我的意思是你可以删除你的帐户(如 Tinder 或 Facebook),应用程序将你弹出,因为你已经被删除,你不再存在。

附带一个 "Delete your account" 的简单按钮。

如果你能帮助我;我还是个新手我真的很感激,我尝试用 Meteor.userId() 检索当前用户的 ID,我正在按以下方式创建一个方法:

Meteor.methods({
  SuprimirPersona: function(id) {
    var postId = Meteor.userId();
    const userId = this.userId;
    const p = Meteor.users.findOne(postId);
    if (userId && p === userId) {
      Meteor.users.remove({
          postId: this._id
        },
        function(error, result) {
          if (error) {
            console.log("Error removing user:", error);
          } else {
            console.log("users removed:" + result);
          }
        })
    }
  }
});

然后按下面的方式调用方法却没有任何结果,我不明白为什么:

'click #Desactivarr': function() {
  var postId = Meteor.userId();
  Meteor.call('SuprimirPersona', userId, function(error, result) {
    if (error) {
      console.log("Somee Error");
    }
  });
  Meteor.logout(function() {
    FlowRouter.go('/');
  });
},

希望有人能帮助我!问候!

从用户集合中删除用户。您需要获取要删除的用户的用户标识。这可以通过在客户端调用 Meteor.userId() 来获取用户的用户 ID 或在服务器上调用 this.userId 来获取。您需要注销用户,在成功注销后,您可以将获得的用户 ID 传递给 meteor.users.remove(userId)

您在客户端和服务器端做了一些不必要的事情 - 例如多次获取相同的用户 ID,甚至没有将其传递给服务器端方法然后再次获取它。

我认为您要做的是获取发布内容的用户的 ID 并将其传递到服务器端,在服务器端检查发布者的 ID 是否与当前用户的 ID 相同。如果是,则删除该用户,否则什么也不会发生。

'click #desactivarr' : function() {
  var postID = <get the id of the user you want to delete here>;
  Meteor.call("suprimirPersona", postID, callbackfunc);
}

那么在服务器端就是

Meteor.methods({
  suprimirPersona : function(postID) {
    var userID = Meteor.userId();
    if (userID && postID === userID) {
      Meteor.users.remove({ postId : userID })
    }
  }
});

Meteor.userId() 和 this.userId return 在客户端执行代码或向服务器端方法发出请求的当前登录用户的 ID。

我刚刚在 Meteor 论坛上回答了这个问题: https://forums.meteor.com/t/how-to-remove-logged-in-user-in-meteor/42639/3

问题是您试图通过 postId 删除用户,而不是 Users.remove({_id: id})。您没有删除任何内容:)