【问题标题】:Cloudant and Php-on-Couch not working well due to continueCloudant 和 Php-on-Couch 无法正常工作,因为要继续
【发布时间】:2016-04-22 04:47:21
【问题描述】:

我相信 Cloudant 最近更改了他们的一些代码。最近,如果您在 try/catch 语句中进行了存储的操作。 Cloudant 将向框架返回一个“错误”:

未捕获的异常“couchException”和消息“继续”

当然你可以在 catch 语句中处理它,但它应该在 PHP-on-Couch 库的 Try 语句中返回为“成功”。

有人遇到过这种情况或知道如何处理吗?最大的问题是您无法在 catch 语句中获取 ID 和 Rev,因为它会作为错误出现:

                try { // does not return here, goes to catch
                    $response = $client->storeDoc($doc);
                    $response_json['status'] = 'success';
                    $response_json['id'] = $response->id;
                    $response_json['rev'] = $response->rev;
                } catch (Exception $e) { // even though the doc is successfully storing

                    // check for accepted BEG
                    $error = '';
                    $error = $e->getMessage();
                    $err_pos = strpos($error,"Accepted");
                    $err_pos_2 = strpos($error,"Continue");
                    if($err_pos !== false OR $err_pos_2 !== false){ // success

                        $response_json['status'] = 'success';
                        $response_json['id'] = $response->id; // returns null
                        $response_json['rev'] = $response->rev; // returns null

                    } else { // truely an error

                        $response_json['status'] = 'fail';
                        $response_json['message'] = $e->getMessage();
                        $response_json['code'] = $e->getCode();

                    }
                    // check for accepted END


                }

【问题讨论】:

    标签: php try-catch cloudant


    【解决方案1】:

    我在 CouchDB 和 Cloudant 中都进行了测试,并且行为是相同的。这就是我相信正在发生的事情。当你创建一个新的 couchDocument 时:

    $doc = new couchDocument($client);
    

    默认情况下,文档设置为自动提交。你可以在 couchDocument.php 中看到这个:

    function __construct(couchClient $client) {
        $this->__couch_data = new stdClass();
        $this->__couch_data->client = $client;
        $this->__couch_data->fields = new stdClass();
        $this->__couch_data->autocommit = true;
    }
    

    在文档上设置属性后:

    $doc->set( array('name'=>'Smith','firstname'=>'John') );
    

    storeDoc 立即被调用。然后您尝试再次调用storeDoc,couchDB 返回错误。

    有两种方法可以解决这个问题:

    1. 关闭自动提交:

      $doc = new couchDocument($client);
      $doc->setAutocommit(false);
      $doc->set( array('name'=>'Smith','firstname'=>'John') );
      try {
          $response = $client->storeDoc($doc);
          $response_json['status'] = 'success';
          $response_json['id'] = $response->id;
          $response_json['rev'] = $response->rev;
      
    2. 设置属性后,保持自动提交并从$doc 获取 id 和 rev:

      $doc = new couchDocument($client);
      try {
          $doc->set( array('name'=>'Smith','firstname'=>'John') );
          $response_json['status'] = 'success';
          $response_json['id'] = $doc->_id;
          $response_json['rev'] = $doc->_rev;
      

    【讨论】:

    • 我尝试了两种方法,未​​捕获的异常 'Exception' 带有消息 'Method setAutocommit does not exist' 和 $doc->set 类似,大概几周到一个月前,一切正常。
    • 这是您使用的库吗? github.com/dready92/PHP-on-Couch。当我回答这个问题时,我刚刚下载了 php 文件,它们对我有用。
    • 啊,原谅我...我需要使用'couch_document class'...稍后将进行更多测试。感谢您的快速回复和解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多