View returns nothing when a key is specified (Couchbase 3.0, Nodejs SDK 2.0)

So assuming there is a record in the database that looks as so:

{
   "ver": "1",
   "src": "google",
   "id": "123"
}

And a View named returnId has the following code:

function (doc, meta) {
  emit(doc.id);
}

And this is what my nodejs code looks like:

var myCluster = new couchbase.Cluster(localhost:8091);
var ViewQuery = couchbase.ViewQuery;
var query = ViewQuery.from('test', 'returnId').key("123");

var buck = myCluster.openBucket('test', function(er){
        if (er) console.log(er);
        buck.query(query, function (err, rows, meta){
            err ? console.log(err) : console.log(rows);
        });
    }
);

When I run this node script against a box with Couchbase 2.2 installed, I get a row result back. However, when I run the same bit of code against a box with Couchbase 3.0 installed, I get no results. The view won’t work in the Couchbase 3.0 GUI either unless I check the box for “inclusive_end”, which seems to default to true in version 2.2 but not 3.0.

Something similar had come up in a different topic, so I’ve created an issue to track it, MB-12446. If you have any other info to add or want to track it, please look to the issue.

Oddly enough, upgrading to couchbase 3.0.1 server seems to have resolved this issue. Running the exact same script as before now returns a row array.

Hey villo,
This is indeed quite odd. Was there anything else that you have changed between your server upgrades? Additionally, are you using XDCR in your server installation?
Cheers, Brett

Hi brett,
There were no other changes as far as I know. The couchbase 2.2 server I was using to test with uses XDCR in a 3-node cluster but in that scenario it was actually returning documents when a key was specified.

I should note that when I tested couchbase 3.0 server I was testing it locally alongside the newest nodejs SDK. I wanted to evaluate both as potential upgrades to our current system. 2.2 seemed to work fine with the new SDK while 3.0 did not, so my assumption was that it was an SDK issue.

Unfortunately, given the different testing setups, I cannot be certain that there were not other factors at play, I only know that the problem has resolved with the newest version of couchbase server.

I am seeing the same issue with ViewQuery.from and noted that the Couchnode script viewquery.js is adding extra quotes around the key value supplied, e.g . var query = ViewQuery.from(‘dev_cag-reg’, ‘MemberIDbyProxyID’).key(12345); becomes …key(’“12345”’);

I can use the REST api and get the doc using ?key=12345 but not “12345”. So I removed the JSON.stringify command in the “key” function of the couchbase script “viewquery.js” and it started to work in the ViewQuery.from logic:

ViewQuery.prototype.key = function(key) {
this.options.key = key; //JSON.stringify(key);
return this;
};

This is obviously a hack and I’d like to know what the correct solution for this issue is in my case. We are running
Version: 3.0.1 Community Edition (build-1444) and couchbase node sdk is version 2.0.5.

Thoughts and comments appreciated.

Thanks

The JSON.stringify function only will apply quotes around string types. This indicates that the value you are passing to the key() method is actually a string rather than a number. Try to parseInt it first!

To further clarify, if you do .key('1234'), it will generate key="1234", if you do .key(1234), it will generate key=1234. Though you’re code seems to indicate you are doing the latter, it would seem that you’re likely actually passing it a variable which is string typed rather than integer typed.

Cheers, Brett

Thank you Brett! That was it…still getting used to javascripts “flexibility”.