title: Fatal Error: "key does not exist on the server"
body: <p>Why does couchbase client for php throw a fatal error</p>
  
  <blockquote>
    <p><strong>Fatal error</strong>: Uncaught exception 'CouchbaseException' with message 'The key does not exist on the server' in [CouchbaseNative]/CouchbaseBucket.class.php on line 196</p>
  </blockquote>
  
  <p>Moreover there is no provision to check whether a key exists on the couchbase or not.</p>
  
  <p>I am writing a wrapper for accessing the couchbase. I am half way through just to read the data from couchbase. But I found this issue; And when I am trying to access a non-existing key it throws fatal error and code crashes (doesn't execute further). </p>
  
  <p>Below is the code (file 'couchbaseWrapper.php'):</p>
  
  <pre><code>&lt;?php namespace CouchBaseWrapper;
   use \CouchbaseCluster;
  
  class CouchDB {    
      private $couchClient;
      private $couchBucket;
  
      private function openConection(){
          try{
              $this-&gt;couchClient = new CouchbaseCluster(&lt;COUCH_HOST&gt;);
              $this-&gt;couchBucket = $this-&gt;couchClient-&gt;openBucket(&lt;COUCH_BUCKET&gt;);
          }
          catch (Exception $e) {
              echo $e;
          }
          return true;
      }
  
      function getKey($k) {    
          try{
              if(!$this-&gt;openConection()) { return false; }
  
              $res = $this-&gt;couchBucket-&gt;get($k);
              $data_rcvd = $res-&gt;value;
          }
          catch(Exception $e){
              echo $e;
          }        
          echo "Outter Code";
          return $data_rcvd;
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>Require &amp; Usage:</p>
  
  <pre><code>&lt;?php namespace CouchBaseWrapper
  
  ini_set("display_errors", true);
  
  require_once('couchbaseWrapper.php');
  
  $cb = new CouchDB();
  $result = $cb-&gt;getKey("non-existing-key"); // &lt;== Error
  //$result = $cb-&gt;getKey("existing-key");   // &lt;== Existing key No error
  
  var_dump($result);
  ?&gt;
  </code></pre>
  
  <p>In case of existing key no issues. It does print data received/fetched and prints the <code>Outter Code</code> as well. But if the request is for non-existing key then it crashes and it does not print the <code>Outter Code</code>.</p>
  
  <p>I have tried the <a href="https://stackoverflow.com/questions/14754007/how-do-i-check-if-key-exists-in-couchbase">exists</a> function. Following is the code (goes in file 'couchbaseWrapper.php'):</p>
  
  <pre><code>public function exists($key) {
      if(!$this-&gt;openConection()) { return false; }
      return $this-&gt;couchBucket-&gt;get($key) == NULL 
      &amp;&amp; $this-&gt;couchBucket-&gt;getResultCode() == COUCHBASE_KEY_ENOENT;
  }
  </code></pre>
  
  <p>Did not get anything out of it. I am using <code>Couchbase Version 2.0.7</code>. Any kind of help is most appreciated.</p>
  
  <p><strong>P.S</strong>: Already read the solution mentioned below; none of them worked</p>
  
  <ul>
  <li><a href="https://forums.couchbase.com/t/get-function-when-doc-id-not-found-crashes-the-code/2352" rel="nofollow noreferrer">Get function when doc id not found crashes the code</a></li>
  <li><a href="https://stackoverflow.com/questions/14754007/how-do-i-check-if-key-exists-in-couchbase">How do I check if key exists in Couchbase?</a></li>
  <li><a href="https://forums.couchbase.com/t/how-to-check-whether-a-document-exists-or-not/3042" rel="nofollow noreferrer">How to check whether a document exists or not</a></li>
  </ul>