N1QL queries give me "Segmentation fault (core dumped)" error

#1

Hi,

Just trying to run my first N1QL query via the node.js (couchnode) SDK and it’s spitting an error at me. Basically just used the method from this page.

Here’s my code:

var couchbase = require("couchbase") ;
var N1qlQuery = require("couchbase").N1qlQuery ;
var myCluster = new couchbase.Cluster('cb4-server1:8091') ;
var myBucket = myCluster.openBucket('quote-unquote') ;
console.log("1") ;

myBucket.enableN1ql(['http://cb4-server1:8093/','http://cb4-server2:8093/']) ;
console.log("2") ;

var nquery = N1qlQuery.fromString("SELECT COUNT(META().id) FROM `quote-unquote` WHERE channels = 'e-quote'") ;
console.log("3") ;

myBucket.query(nquery, function(err, results) {
console.log("4") ;

    if(err) 
    {
        console.log("query failed", err) ;
        return ;
    }
    console.log("success!", res) ;

});

And here’s the console output:

~$ nodejs UpdateGuesses.js
1
2
3
Segmentation fault (core dumped)

The query works fine when I use the command line (cbq). Any ideas about what’s going on?

Kind regards,
David

#2

Hey @dbergan,

What version of Couchbase Server and what version of the Node.js SDK are you using?

Best,

#3

CB Server is 4.0 release candidate.

Couchnode was installed 2 days ago via

sudo npm install couchbase

Let’s see, how do I find the version number again…

#4

npm -v couchbase
2.11.3

#5

oops… that’s the version of npm

ok, I did
npm list

couchbase@2.1.0

#6

From the couchnode computer, when I do this:

curl http://cb4-server1:8093/

I get this:

404 page not found

However at port 8092 I get:

{"couchdb":"Welcome","version":"","couchbase":"4.0.0-4047-community"}
#7

Hey @dbergan,

Out of curiosity can you remove the following line?:

myBucket.enableN1ql(['http://cb4-server1:8093/','http://cb4-server2:8093/']);

You shouldn’t need it with the latest Node.js SDK and Couchbase 4.x. Depending on the results, we’ll go from there.

Best,

#8

Something new!

Now it says

~$ nodejs UpdateGuesses.js
1
2
3
4
query failed { [Error: Unable to decode prepared statement - cause: Unrecognizable prepared statement - cause: unable to convert to prepared statment.]
  code: 4070,
  otherErrors: [],
  requestID: '0a0f6c86-a439-44ca-8b03-00deb794a32c' }
#9

We’re doing better :slight_smile:

var nquery = N1qlQuery.fromString("SELECT COUNT(META().id) FROM `quote-unquote` WHERE channels = 'e-quote'") ;

I think the problem might be with your use of the META command. You need to provide a bucket name that you wish to use. What happens if you change your query to the following:

var nquery = N1qlQuery.fromString("SELECT COUNT(META(qu).id) FROM `quote-unquote` AS qu WHERE qu.channels = 'e-quote'") ;

Notice that I gave your bucket an alias and plugged it into the META function.

Let me know how that goes.

Best,

#10

You might also try providing an empty array for prepared statement parameters. Example:

myBucket.query(nquery, [], function(err, results) {

Beneficial to you in the future to prevent SQL injection on user defined query parameters.

Best,

#11

Suggestion 1 (putting the bucket in the META) results in the exact same error. I attempted it with both the full bucket name quote-unquote and the alias. All resulted in error 4070.

Suggestion 2 (using an empty array) gives me an all new error.

myBucket.query(nquery, function(err, [], results) {
                                     ^
SyntaxError: Unexpected token [

I get this error regardless of how I handle the META suggestion.

Kind regards,
David

#12

Hey @dbergan,

The brackets are in the wrong spot. They should come after nquery, but before the function.

bucket.query(query, prepared_parameters_array, callback_function)

Let me know how that works out.

Best,

#13

As soon as I posted this, I realized I put the [] in the wrong place. When I put it in the right place, i.e.

myBucket.query(nquery, [], function(err, results) {

I no longer get the syntax error, but still do get the 4070 errors with/without the META suggestions.

#14

Hey @dbergan,

I’m going to pass this issue on to one of our Node.js SDK engineers. I will get back to you as soon as I get a response!

Best,

#15

Sounds good. I updated couchnode to the github master.

sudo npm install "git+https://github.com/couchbase/couchnode.git#master"

It’s still giving me the same error.

#16

@nraboy

Heard anything from the engineers yet?

#17

Hey @dbergan,

Thanks for bringing this up again. Let me ping them once more :smile:

Best,

#18

Hey @dbergan,

Please install the latest version of the SDK (2.1.1) from npm. There was an issue with statement preparation which would cause issues like you were seeing.

Cheers, Brett

#19

Awesome. Works great now! Thanks so much.

#20

Hey @dbergan, I’m glad our Node.js engineer, @brett19, was able to find the solution!

Reach out in the forums if you need anything else in the future.

Best,