N1QL Query with Consistency set to REQUEST_PLUS

Can someone please advise how to use the Consistency and REQUEST_PLUS ? In the 2.X SDK that was simple but a lot of things changed and are not very well documented.

Below is my old 2.x aproach

const N1qlQuery = couchbase.N1qlQuery

let statement = N1QLQuery.fromString(queryStr)
statement.consistency(N1QLQuery.Consistency.REQUEST_PLUS)
var result = await cluster.query(statement, id)
console.log(result)

in 3.x it should look something like this but {scanConsistency : RequestPlus} is not valid

var result = await cluster.query(queryStr,id , {scanConsistency : RequestPlus})

Hey @aponnath,

I think the invocation you are looking for would be the following:

var result = await cluster.query(queryStr, {
  scanConsistency: couchbase.QueryScanConsistency.RequestPlus,
});

Cheers, Brett

1 Like

Thanks that value at least gets me in the right direction and is for sure missing in the docs.
Now when i use
var result = await cluster.query(queryStr,id ,{scanConsistency : couchbase.QueryScanConsistency.RequestPlus})

i get the result as expected but also the folowing error

(node:14752) UnhandledPromiseRejectionWarning: TypeError: callback is not a function
at C:\Users\Alex Ponnath\Documents\GitHub\New-Couchbase\node_modules\couchbase\lib\promisehelper.js:79:25
(node:14752) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14752) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

when i remove the {scanConsistency : couchbase.QueryScanConsistency.RequestPlus} from the code it completes and returns also the data without the error ? Any reason for this behavior ?

Hey @aponnath,
It looks like there is an issue where the callback function is being invoked in spite of the fact that you have not specified one, though looking at the code, this case is guarded against. I’ll have to take a look and see what’s going on, I’ve opened an issue to track this: https://issues.couchbase.com/browse/JSCBC-672 .
Cheers, Brett

1 Like

Remove the ‘id’ argument in your call.

async query(query, options, callback)

if i remove the id, i have no way to pass my query parms for the query

Hey @aponnath,

Sorry, I completely missed that you were passing an id parameter to you query call. This would be the correct code to do that with the new API of the 3.0 SDKs:

var result = await cluster.query(queryStr, {
  scanConsistency: couchbase.QueryScanConsistency.RequestPlus,
  parameters: [id],
});

P.S. You can find the API reference documentation for the query method here:
https://docs.couchbase.com/sdk-api/couchbase-node-client-3.0.0/Cluster.html#query

Cheers, Brett

1 Like

Chaining a ‘catch()’ on to the cluster.query() call will eliminate the UnhandledPromiseRejectionWarning

var result = await cluster.query(queryStr,{scanConsistency: couchbase.QueryScanConsistency.RequestPlus}).catch((e) => { console.log(e); })

actually as the syntax has changed in 3.0 i can just use this one as brett said

var result = await cluster.query(queryStr,{ parameters: [id], scanConsistency : couchbase.QueryScanConsistency.RequestPlus})

this executes fine and no longer throws an error. But looking at the current 3.0 docs that’s not that clear.

var result = await cluster.query(
    'SELECT x.* FROM `default` WHERE x.Type=$0', ['User'])

Does it mean if i have no other Options i can specify like cluster.query( queryStr, [parameter]) or is this a typo in docs ?

Actually looking at the docs i see even with other option there seems to be wrong syntax in docs

var result = await cluster.query( 'SELECT x.* FROM `default` WHERE x.Type=$1', ['User'], {
        consistentWith: state,
    });

Those look to be incorrect. They are from https://docs.couchbase.com/nodejs-sdk/3.0/howtos/n1ql-queries-with-sdk.html

https://issues.couchbase.com/browse/DOC-6227