【问题标题】:Meteor.user() not reactive at Meteor.users.removeMeteor.user() 在 Meteor.users.remove 上没有反应
【发布时间】:2017-07-02 00:24:28
【问题描述】:

目前我正在开发一个功能,以便用户可以删除他或她自己的帐户。我正在使用react-meteor-data 响应式检索用户对象。

现在我可以在服务器上调用我的方法时成功地从用户集合中删除用户,但是删除用户后客户端上的Meteor.user() 对象不再是反应性的。有什么我需要关心的步骤吗?我在想我可以这样做:

  1. 用户删除他/她自己的帐户。
  2. 用户在用户集合中被删除并将被注销/或者Meteor.user() 在删除用户后是被动的

到目前为止我做了什么:

容器

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() 不再更新。

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

提前致谢

【问题讨论】:

    标签: meteor meteor-accounts


    【解决方案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;
    

    【讨论】:

    • 感谢您的回答。我试过你的方法,但不幸的是它不起作用。如果我从集合中删除用户,我的整个客户端似乎都卡住了。使用“流星玩具”,用户仍处于登录状态,订阅也没有更新..
    • 如果你删除了流星玩具包会发生什么(不打算在产品中使用,所以只是为了检查是否有干扰)?
    • 问题依然存在。我不明白为什么只有 Meteor.user() 对集合没有反应。
    • 哦,我刚刚在你的代码中看到了一些东西:Meteor.users.remove(this.userId); 你可以控制台记录它,它返回真还是假?它不一定会在失败时抛出错误,而只是返回 false 或 undefined 之类的。
    • 它返回 user._id 和 '1'。所以用户肯定会被删除,但只有客户端没有相应地更新。如果我重新加载页面,我将退出并且无法再次登录。
    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 2021-11-11
    • 2015-04-26
    • 1970-01-01
    相关资源
    最近更新 更多