Meteor.user()在Meteor.users.remove

时间:2017-07-01 13:11:41

标签: meteor meteor-accounts

目前我正在处理一项功能,以便用户可以删除自己的帐户。我正在使用react-meteor-data来反复检索用户对象。

到目前为止,我可以在服务器上调用my方法时成功从用户集合中删除用户,但删除用户后,客户端上的Meteor.user()对象不再被激活。我有什么必须关心的步骤吗?我以为我可以这样做:

  1. 用户删除自己的帐户。
  2. 用户集合中的用户被删除,并且在删除用户后将被注销/或Meteor.user()被拒绝
  3. 到目前为止我做了什么:

    容器

    import { Meteor } from 'meteor/meteor';
    import { createContainer } from 'meteor/react-meteor-data';
    
    import AccountDetail from "../components/user/AccountDetail.component.jsx";
    
    export default EventDetailContainer = createContainer(({ id }) => {
        const user = Meteor.user();
        const selfDelete = () => {
            Meteor.call("selfDelete", error => {
                if (!error) {
                    console.log("Success self delete");
                } else {
                    console.log("fail self delete");
    
                }
            });
        }
        return {
            user,
            selfDelete
        };
    }, AccountDetail);
    

    服务器方法

      Meteor.methods({
            selfDelete() {
                console.log(this.userId);
                try {
                    Meteor.users.remove(this.userId);
                } catch (e) {
                    throw new Meteor.Error('self-delete', 'Failed to remove yourself');
                }
            }
        });
    

    因此,在调用selfDelete()后,用户将从集合中删除,但客户端Meteor.user()不再更新。

    也许有人知道为什么会这样,或者你知道让用户删除自己帐户的更好方法。

    提前致谢

1 个答案:

答案 0 :(得分:1)

更改

const user = Meteor.user();

const user = Meteor.users.findOne(Meteor.userId());

这使得用户依赖于集合和构建跟踪器来重新运行反应容器中的函数。评论如果没有按预期工作。适用于我的容器。

编辑:忘了说,你需要在容器中订阅和发布Meteor.users才能使这项工作成功。那么你的完整代码就是:

//server
Meteor.publish("myuserssub", function(filter){
    if (filter._id !== this.userId)
        throw new Error("not permitted");
    return Meteor.users.find(filter);
});

//client
const userSub = Meteor.subscribe("myuserssub");
const user = userSub ? Meteor.users.findOne({_id:Meteor.user()._id}) : null;