Couchbase connection issues

the Couchbase database is up, but when my api service tries to connect it fails. I have to restart my api service multiple times before it reconnects. Even thought coucbhase database is up the whole time. Is there a fix for this? On another machine it reconnects no problem. Is there some logic I need to build to handle the connection better?

Error stack:
Error: parent cluster object has been closed
    at Connection._maybeBFwd (/<user_directory>/node_modules/couchbase/lib/connection.js:181:13)
    at Connection.get (/<user_directory>/node_modules/couchbase/lib/connection.js:216:10)
    at /<user_directory>/node_modules/couchbase/lib/collection.js:224:18
    at /<user_directory>/node_modules/couchbase/lib/promisehelper.js:30:7
    at new Promise (<anonymous>)
    at Function.wrap (/<user_directory>i/node_modules/couchbase/lib/promisehelper.js:29:12)
    at Collection.get (/<user_directory>/node_modules/couchbase/lib/collection.js:223:26)
    at /<user_directory>/src/api/lookups/visibility/route.ts:63:33
    at Layer.handle [as handle_request] (/<user_directory>/node_modules/express/lib/router/layer.js:95:5)

Hey @Michael_Sor,

Can you try to enable debugging (env var: DEBUG=*) and see what the logs show as the reason for the failure to connect?

Cheers, Brett

Hey Brett I work with Mike and have the logs here with debug enabled

express:router dispatching GET /users +4s
  express:router query  : /users +3ms
  express:router expressInit  : /users +1ms
  express:router <anonymous>  : /users +1ms
  express:router helmet  : /users +2ms
  express:router jsonParser  : /users +1ms
  express:router urlencodedParser  : /users +1ms
  express:router corsMiddleware  : /users +1ms
  express:router router  : /users +4ms
  express:router dispatching GET /users +1ms
  express:router trim prefix (/users) from url /users +2ms
  express:router router /users : /users +2ms
  express:router dispatching GET / +1ms
Error: parent cluster object has been closed
    at Connection._maybeFwd (C:\Projects\api\node_modules\couchbase\lib\connection.js:156:13)
    at Connection.query (C:\Projects\api\node_modules\couchbase\lib\connection.js:260:10)
    at QueryExecutor.query (C:\Projects\api\node_modules\couchbase\lib\queryexecutor.js:109:16)
    at Cluster.query (C:\Projects\api\node_modules\couchbase\lib\cluster.js:249:24)
    at router.get (C:\Projects\api\src\api\users\route.ts:31:32)
    at Layer.handle [as handle_request] (C:\Projects\api\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Projects\api\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Projects\api\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Projects\api\node_modules\express\lib\router\layer.js:95:5)
    at C:\Projects\api\node_modules\express\lib\router\index.js:281:22
[2020-04-16T11:55:28.086] [ERROR] jllis-api - ::1 - - "GET /users HTTP/1.1" 400 2 "http://localhost:9090/docs/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"

Hey @lauanthony,

Which version of the Node.js SDK are you using? I would have expected far more logging from libcouchbase there.

Cheers, Brett

Hi Brett,

Our dependencies below. We are using a simple connection:

config/couchdb.ts
export const couchbase = require(‘couchbase’)
let conn = ${config.database.database}://${config.database.host}
export const cluster = new couchbase.Cluster(conn, {
username: config.database.username,
password: config.database.password
});

Route.ts
var bucket = cluster.bucket(‘user’)
var coll = bucket.defaultCollection()

Couchbase Server version: Enterprise Edition 6.5.0 build 4960 ‧ IPv4 (Docker Image)

“dependencies”: {
@types/cors”: “^2.8.6”,
@types/couchbase”: “^2.4.1”,
@types/dotenv”: “^8.2.0”,
@types/express”: “^4.17.3”,
@types/helmet”: “0.0.45”,
@types/node”: “^13.9.3”,
@types/swagger-jsdoc”: “^3.0.2”,
@types/uuid”: “^3.4.7”,
“cors”: “^2.8.5”,
“couchbase”: “^3.0.1”,
“dotenv”: “^8.2.0”,
“express”: “^4.17.1”,
“helmet”: “^3.21.3”,
“log4js”: “^6.1.2”,
“swagger-jsdoc”: “^3.5.0”,
“swagger-ui-express”: “^4.1.3”,
“typescript”: “^3.8.3”

Hey @lauanthony,

For SDK 2.x , you should be able to add an event handler to bucket to watch for errors that occur after the open-bucket call. You can alternatively register for a callback from openBucket which will provide the same information.

var bucket = cluster.openBucket('default')
bucket.on('error', (err) => {
  console.log('connect failed:', err)
})

Cheers, Brett

Hey Brett,
Not sure if we’re on the wrong SDK version or something, We are using
cluster.bucket(‘default’)
It will then throw an error saying bucket.on is not a function when I add the code snippet you provided.

So I tried changing that to cluster.openBucket but it’s not finding the openBucket function

TypeError: couchdb_1.cluster.openBucket is not a function

Is it an issue if we are using SDK v3.0.1 but using type definition with v2.4.1?

“couchbase”: “^3.0.1”,
@types/couchbase”: “^2.4.1”,

Hi brett, I work with Mike and Anthony We are using, like Mike pointed out,
“couchbase”: “^3.0.1”,
@types/couchbase”: “^2.4.1”,

I am new to this environment (Node.js and Couchbase)
We use TypeScript.
I noted that the import of couchbase done in this what is not giving intellisense

export const couchbase = require('couchbase')
export const  cluster = new couchbase.Cluster(conn, {
  username: config.database.username,
  password: config.database.password
});

// Open a specific Couchbase bucket, user in this case.
var bucket = cluster.bucket(‘user’)

But in the sample “couchebase-nodejs-6.5” we get intellisense up to cluster after that all variable are defined as “any”

I noted that if I define cluster of type :Cluster, node adds the following import

*import { Cluster } from 'couchbase';*
export const  cluster**: Cluster** = new couchbase.Cluster(conn, {

And the rest of the code breaks

// Open a specific Couchbase bucket, `user` in this case.
var bucket = cluster.bucket('user')

bucket does not exist

But we get intellisense

Which approach should we use

I have been working with the new SDK 3.0.1 for a couple of days and so far have learned and correct me if I’m wrong:
The @types /couchbase file version 2.4.1 is a couple of years old.
SDK 3.0.1 has significant changes from those types with the introduction on promises.
I found this tool to help create my own @types file .


I created a folder in my SRC folder in Nodejs named @types\couchbase and added the .d.ts file there.
I’m working on modifying the file because it creates a lot of “any” types. This a slow process for me because of my lack of programming experience .
If anyone has a completed .d.ts file for the SDK 3.0.1 and is willing to share it. That would be fantastic.

Ron - You are correct. Typescript 2.4.1 is a few years old. We had a feeling this could cause issues going forward. If anyone created a .ts file that would be great. We just start our development. So we weren’t sure how much of a difference 2.4.1 and SDK 3.0.1. But to be safe we shouldn’t be using @types/couchbase 2.4.1 with SDK 3.0.1.

The SDK 2.6.11 works with @types 2.4.1 . You will be able to use Couchbase with Nodejs and Typescript. Promises and collections wont work though.

I reinstalled nodeJS and restarted my computer and now its now spitting out a bit more information for some reason on startup of our app. I am not sure where that path is coming from since I don’t have jenkins installed anywhere.

 couchnode:lcb:error (bootstrap @ c:\jenkins\workspace\nodejs\couchnode\couchnode-scripted-build-pipeline\couchnode\deps\lcb\src\bootstrap.cc:196) Failed to bootstrap client=000002665AE792B0. Error=LCB_ERR_TIMEOUT (201), Message=Failed to bootstrap in time +0ms
  couchnode failed to connect to bucket: [Error: LCB_ERR_TIMEOUT (201): A request cannot be completed until the user-defined timeout fired] {
  couchnode   code: 201
  couchnode } +0ms
  couchnode:lcb:error (bootstrap @ c:\jenkins\workspace\nodejs\couchnode\couchnode-scripted-build-pipeline\couchnode\deps\lcb\src\bootstrap.cc:196) Failed to bootstrap client=000002665AE776B0. Error=LCB_ERR_TIMEOUT (201), Message=Failed to bootstrap in time +0ms
  couchnode failed to connect to bucket: [Error: LCB_ERR_TIMEOUT (201): A request cannot be completed until the user-defined timeout fired] {
  couchnode   code: 201
  couchnode } +26ms
  couchnode:lcb:error (bootstrap @ c:\jenkins\workspace\nodejs\couchnode\couchnode-scripted-build-pipeline\couchnode\deps\lcb\src\bootstrap.cc:196) Failed to bootstrap client=000002665AE778B0. Error=LCB_ERR_TIMEOUT (201), Message=Failed to bootstrap in time +0ms
  couchnode failed to connect to bucket: [Error: LCB_ERR_TIMEOUT (201): A request cannot be completed until the user-defined timeout fired] {
  couchnode   code: 201
  couchnode } +28ms
  couchnode:lcb:debug (lcbio_mgr @ c:\jenkins\workspace\nodejs\couchnode\couchnode-scripted-build-pipeline\couchnode\deps\lcb\src\lcbio\manager.cc:503) <localhost:11210> (HE=000002665ACA9560) Request=00000266580C1A40 has no connection.. yet +0ms
  couchnode:lcb:debug (lcbio_mgr @ c:\jenkins\workspace\nodejs\couchnode\couchnode-scripted-build-pipeline\couchnode\deps\lcb\src\lcbio\manager.cc:503) <localhost:11210> (HE=000002665ACA99E0) Request=00000266580C18C0 has no connection.. yet +0ms
  couchnode:lcb:debug (lcbio_mgr @ c:\jenkins\workspace\nodejs\couchnode\couchnode-scripted-build-pipeline\couchnode\deps\lcb\src\lcbio\manager.cc:503) <localhost:11210> (HE=000002665ACA9E60) Request=00000266580C2640 has no connection.. yet +0ms