简体   繁体   English

Collection2,使用方法插入,未捕获唯一约束的异常

[英]Collection2, insert using method, exception from unique constraint not caught

I create a new project: 我创建一个新项目:

$ mrt create sandbox
$ mrt remove autopublish
$ mrt add collection2

And use the following code to create a simple collection with a unique constraint on a key 并使用以下代码创建对键具有唯一约束的简单集合

SandBoxCollection = new Meteor.Collection('sandboxcoll', {
  schema: new SimpleSchema({
    title: {
      type: String,
      min: 3,
      unique: true,
      index: true
    }
  })
});

if (Meteor.isServer) {
  Meteor.publish('sandboxpub', function() {
    return SandBoxCollection.find();
  });
}

if (Meteor.isClient) {
  Meteor.subscribe('sandboxpub');
}

Meteor.methods({
  create: function(doc) {
    var docId = SandBoxCollection.insert(doc, {validationContext: 'create'}, function(err, res) {
      if (err) {
        throw new Meteor.Error(333, SandBoxCollection.simpleSchema().namedContext('create').invalidKeys());
      }
      return res;
    });
    return docId;
  }
});

I set up a simple collection, pub/sub and a method that I can use for inserts. 我设置了一个简单的集合,pub / sub和一个可用于插入的方法。

Then I use the browser console to issue the following commands 然后,我使用浏览器控制台发出以下命令

Let's first create a document: 首先创建一个文档:

Meteor.call('create', {title: 'abcd01'}, function(e,r){
  console.log(e ? e : r);
});

Now let's try inserting a duplicate directly using collection.insert(): 现在,让我们尝试使用collection.insert()直接插入副本:

SandBoxCollection.insert({title: 'abcd01'}, function(e,r) {
  console.log('error: ');
  console.log(e);
  console.log('errorkeys: ');
  console.log(SandBoxCollection.simpleSchema().namedContext().invalidKeys()); 
  console.log('result: ');
  console.log(r);  
});

We can see a proper 333 error handled by the callback and logged to the console. 我们可以看到由回调处理并记录到控制台的适当的333错误。

Now try inserting a duplicate using the method: 现在尝试使用方法插入副本:

Meteor.call('create', {title: 'abcd01'}, function(e,r){
  console.log(e ? e : r);
});

Notice that, unlike the direct insert, the method throws an uncaught exception! 请注意,与直接插入不同,该方法将引发未捕获的异常! Furthermore, the error is thrown from our custom throw and it has error code 333. 此外,该错误是从我们的自定义引发中引发的,错误代码为333。

Why is this not handled properly? 为什么这不能正确处理? What can I do to mitigate this so that I can do something with the error (notify the user, redirect to the original documents page etc) 我可以采取什么措施来减轻这种情况,以便我可以对错误进行处理(通知用户,重定向到原始文档页面等)

As of February 2014, this is an enhancement request on collection2 issue tracker at https://github.com/aldeed/meteor-collection2/issues/59 自2014年2月起,这是对collection2问题跟踪程序的增强请求, 网址https://github.com/aldeed/meteor-collection2/issues/59

The current workaround (on the server) is to catch the error separately and feed it into a custom Meteor.Error as in: 当前的解决方法(在服务器上)是分别捕获错误,并将其输入到自定义Meteor.Error中,如下所示:

if (Meteor.isServer) {
  Meteor.methods({
    insertDocument: function(collection, document) {
      check(collection, String);
      check(document, Object);

      var documentId = '',
          invalidKeys = [];

      function doInsert() {
        documentId = SandboxProject.Collections[collection + 'Collection'].insert(document, {validationContext: collection + 'Context'});
      }

      try {
        doInsert();
      } catch (error) {
        invalidKeys = SandboxProject.Collections[collection + 'Collection'].simpleSchema().namedContext(collection + 'Context').invalidKeys();
        error.invalidKeys = invalidKeys;
        throw new Meteor.Error(333, error);
      }
      return documentId;
    }
  });
}

Note: This is a generic insert method that takes the namespaced collection name as a parameter and a document. 注意:这是一个通用的插入方法,它将命名空间的集合名称作为参数和文档。 It is intended to be called from the client side with a callback function which returns either the result as a document id or an error object. 它打算通过一个回调函数从客户端调用,该函数返回结果作为文档ID或错误对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM