目前我正在处理一项功能,以便用户可以删除自己的帐户。我正在使用react-meteor-data
来反复检索用户对象。
到目前为止,我可以在服务器上调用my方法时成功从用户集合中删除用户,但删除用户后,客户端上的Meteor.user()
对象不再被激活。我有什么必须关心的步骤吗?我以为我可以这样做:
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()
不再更新。
也许有人知道为什么会这样,或者你知道让用户删除自己帐户的更好方法。
提前致谢
答案 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;