Error: cluster object was closed

I am simulating a retrieve function from our database in couchbase and when I try to retreive the data it is showing me this. I’ve search throughout the net on how to solve this but found none.

Here is the error

Error --------------------------------------------------

Error: cluster object was closed
at Connection.close (/Users/franzdayrit/_Nobel systems/Projects/valencia-billing/node_modules/couchbase/lib/connection.js:266:24)
at /Users/franzdayrit/_Nobel systems/Projects/valencia-billing/node_modules/couchbase/lib/cluster.js:612:14

Here is my code using serverless along with couchbase and nodejs

handler.js:

'use strict';

const couchbase = require('couchbase');

//Create base Cluster connection
let cluster = new couchbase.Cluster("couchbase://xxx", {
  username: "xxx",
  password: "xxx"
});

let bucket = cluster.bucket('xxx');

module.exports.retrieve = (event, context, callback) => {
  let qs = "SELECT * FROM xxx WHERE billstatus = 'paid'"
  cluster.query(qs, (error, result) => {
    if(error) {
      return callback({
        statusCode: 500,
        body: JSON.stringify({
          code: error.code,
          message: error.message
        })
      }, null)
    }
    callback(null, {
      statusCode: 200,
      body: JSON.stringify(result)
    })
  })
}

serverless.yml:

service: xxx

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x

functions:
  hello:
    handler: handler.hello
    events:
      - http: GET hello
  retrieve:
    handler: handler.retrieve
    events:
      - http: GET retrieve


plugins:
  - serverless-offline

Hello @aeo03 welcome to the forums !

What is your environment ? Is it on AWS Lambda (looking at serverless.yml) ? And what version of SDK are you using ?

Hi @AV25242 thanks for replying,

As of now, I’m creating a mini app in my local machine. So it is just local. For the SDK ver., I just installed in our CLI.

Thanks @aeo03 I am assuming that you using Couchbase SDK 3.x

I see you are not awaiting on the query part and are using callbacks remember SDK 3.x uses promise
I am assuming you have the right indexes on billstatus and your query returns faster when tried using Couchbase Query workbench

try { let query = "SELECT * FROM xxx WHERE billstatus = 'paid'" let result = await cluster.query(query) console.log("Result:", result) return result } catch (error) { console.error('Query failed: ', error) }

so to avoid any confusions the latest version of Couchbase Node.js SDK is 3.1, if possible for you to download that and use (may not be needed but doesn’t hurt) ?

Hi @AV25242

I have a similar problem but is intermittent. Me use a diagnostics to check if cluster are connected:

    const cluster = new couchbase.Cluster(cString, {
        username,
        password,
        kvTimeout: opTimeout,
        connectTimeout: connTimeout
    });

    const bucket = cluster.bucket(bucketName);
    const collection = bucket.defaultCollection()


    cluster.diagnostics().then(res => {
        console.log('connect');
    }).catch(err => {
        console.log('error', err.stack);
    });

@laurentiustroia welcome to the forums.

Your issue is related to Cluster closed - reinitialize connection? - #3 by mmiguel89

There is an issue created to fix this problem.

Hi, thank you.

Look very similar with that problem, but in my cases the couchbase server is up and running before app, so I exclude a delay until couchbase are starting, but a strang behaviour is if I restart that app then all working perfectly. Can be another delay until the connection is established and that work to be cached somewhere and when restart just reuse it ?

If a retry mechanism will be implemented in 3.1.1 would be great.

It’s a bit sad that I run into this problem following Install and Start Using the Node.js SDK with Couchbase Server | Couchbase Docs
Has this not been tested?

Hi Mike_Reys can you please let us know what is that which was not working for you ? error etc ?