Collection.exists("some id that has never been added and should not exist in the collection") returns true

After connecting to my Couchbase cluster and letting ‘collection’ be defined as an existing collection in an existing bucket on Couchbase Server version 7.1 or defined as the default collection of an existing bucket on Couchbase Server version 6.6:

> await collection.exists("some id that has never been added and should not exist in the collection")
ExistsResult { exists: true, cas: Cas<0> }

The result is the same with any id I try that has never been added.

After some digging I found that in lib/collection.ts, in the exists function, resp has a property document_exists which is correctly shown as false, yet no DocumentNotFoundError is thrown. There probably should be a fix to make it throw the DocumentNotFoundError.

In the meantime, a fix for this collection.exists function behavior is to edit the if statement

if (resp.deleted) {
    return wrapCallback(null, new crudoptypes_1.ExistsResult({
        cas: undefined,
        exists: false,
    }));
}

by appending a check for resp.document_exists being false, like so

if (resp.deleted || !resp.document_exists) {
    return wrapCallback(null, new crudoptypes_1.ExistsResult({
        cas: undefined,
        exists: false,
    }));
}

This makes the collection.exists function correctly return exists: false. However, I don’t know if this simple line edit is sufficient to ensure correct behavior of the exists function in all situations.
For example, this would be incorrect if the intended cas value of a not yet created document is in fact cas: Cas<0> and not cas: undefined. Also, as stated above, a DocumentNotFoundError should probably be thrown and caught by the appropriate code within the exists function error handling.

I’m using Couchbase NodeJS SDK v4.1.3

Thank you

hi - I posted a note about this on JSBC-1006 for the developer.

1 Like

Update: this issue has been resolved as of version 4.2.0 of Couchbase NodeJS SDK.