Transactions ArangoDB事务-如何防止引发异常

Transactions ArangoDB事务-如何防止引发异常,transactions,acid,arangodb,Transactions,Acid,Arangodb,如何防止ArangoDB在查找特定文档时在事务期间引发异常,而该文档当时可能不存在 Nodejs在一个块中将事务发送到ArangoDb,并在那里对其进行处理。太好了。我想将所有数学卸载到服务器 在交易过程中,我想查看特定的集合,检查文档是否存在,是否可以找到文档,然后获取字段“余额”,但如果找不到文档或其字段,则我不想引发异常,也不想停止正在进行的交易。相反,我更希望继续处理事务,我们将变量oldBalance指定为字符串“0” (仅供参考:在nodeJS端指定了集合“user”的写锁) 在这里

如何防止ArangoDB在查找特定文档时在事务期间引发异常,而该文档当时可能不存在

Nodejs在一个块中将事务发送到ArangoDb,并在那里对其进行处理。太好了。我想将所有数学卸载到服务器

在交易过程中,我想查看特定的集合,检查文档是否存在,是否可以找到文档,然后获取字段“余额”,但如果找不到文档或其字段,则我不想引发异常,也不想停止正在进行的交易。相反,我更希望继续处理事务,我们将变量oldBalance指定为字符串“0”

(仅供参考:在nodeJS端指定了集合“user”的写锁) 在这里,您可以看到发送到ArangoDB的部分事务代码:

var db = require('internal').db;
// 1.) find specific document
var accountdoc = db.user.document('Johnny04'); // find doc by _key
如果找不到具有该特定_键的文档,则会引发异常。此时,用户可能在集合中没有条目。在这种情况下,我们要假设他的余额为字符串“0”。但不幸的是,已经抛出了一个异常。我更希望继续进行以下工作:

//2.) calculate newBalance = oldBalance + additional
        if (accountdoc.error==true){ // document not found etc...
            var oldBalance='0';
            var documentExists = false;
        } else {
            var oldBalance=accountdoc.balance;
            var documentExists = true;
            var documentExistsID = accountdoc._id;
        }   
您不能像这样处理事务中的“未找到文档”错误:

function (params) {
  var db = require("org/arangodb").db;
  var accountdoc;

  // 1.) find specific document
  try {
    accountdoc = db.user.document('Johnny04'); // find doc by _key
  }
  catch (err) {
    // document not found etc.
    // TODO: rethrow exception if err is something different than "document not found"
  }

  // 2.) calculate newBalance = oldBalance + additional
  if (accountdoc === undefined) { // document not found etc...
    // create a new document with balance 0
    db.user.save({ _key: 'Johnny04', balance: '0' }); // note: if this fails, the transaction will throw
  } 
  else {
    // update the existing document
    var oldBalance = accountdoc.balance;
    var newBalance = oldBalance + 42;
    db.user.update('Johnny04', { balance: newBalance }); // note: if this fails, the transaction will throw
  }   
}

哇,真是太棒了。多亏了你的试抓拦网,它才起作用。