【问题标题】:Meteor: Uncaught Error: Must be attached (delete function)流星:未捕获的错误:必须附加(删除功能)
【发布时间】:2016-09-14 12:28:52
【问题描述】:

每次我在 Meteor 应用程序中删除项目(列表)时,控制台都会出现错误。 控制台中的错误是:

domrange.js:337 未捕获的错误:必须附加

这是函数,我不明白这个错误是从哪里来的:

Lists.js

Meteor.methods({
   'lists.remove'(listId) {
       check(listId, String);

       const list = Lists.findOne(listId);
       if (list.owner !== this.userId) {
           throw new Meteor.Error('not-authorized');
       }
       Tasks.remove({"listId": listId});
       Lists.remove(listId);
   },

在应用程序中一切正常,但你知道这个错误可能来自哪里吗?

Ps:如果有帮助,我正在使用 Blaze

谢谢

【问题讨论】:

  • 看起来这是一个已知的流星问题 - github.com/meteor/meteor/issues/2981
  • 感谢@Craicerjack,我以前读过这篇文章,只是这个话题是从 2015 年开始的,所以我认为可能有一个新的解决方案。似乎没有解决方案,只是一些补丁,我不知道如何在我的代码中使用 Meteor.defer() 解决方案。
  • 您可以创建2个版本的方法,一个用于客户端,一个用于服务器(或者根本不在客户端实现该方法)。您可以创建 2 种不同的方法或使用 this.isSimulation 仅在客户端/服务器上运行部分代码。

标签: javascript meteor uncaught-exception typeerror


【解决方案1】:

似乎我找到了添加 Meteor.isServer 或更好的解决方案(!this.isSimulation)(@MasterAM 解决方案):

    'lists.remove'(listId) {
        check(listId, String);

        const list = Lists.findOne(listId);
        if (list.owner !== this.userId) {
            throw new Meteor.Error('not-authorized');
        }
        if (!this.isSimulation) {
            Tasks.remove({"listId": listId});
            Lists.remove(listId);
        }
    },

我在@MasterAM 的帮助下编辑了工作代码 现在可以工作了!没有控制台错误了。

【讨论】:

  • this.isSimulation 在客户端计算为true(在存根中),所以如果你想防止这种情况在存根中发生,你应该使用if (!this.isSimulation)
  • 哦,好吧,我明白了!非常感谢@MasterAM 的解释 :)
猜你喜欢
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2017-07-09
  • 2018-03-22
相关资源
最近更新 更多