I’m going through the node.js sdk training and I am stuck on lab 4. The training seems to be out of date, but I was able to figure out lab 3. But on lab 4, I can’t seem to get the query to work. I keep getting errors about the query function being undefined. I’ve tried on bucket, and collection. Here’s my code.
var couchbase = require("couchbase");
var express = require("express");
var bodyParser = require('body-parser');
var port = process.env.port || 3000;
// Open bucket and create bucket reference for use in routes
var cluster = new couchbase.Cluster("127.0.0.1:8091", {
username: 'Customer360',
password: 'password'
})
var bucket = cluster.bucket("Customer360")
var collection = bucket.defaultCollection()
var app = express();
var jsonParser = bodyParser.json();
app.get("/country/:id", async function(request, response) {
var docId = request.params.id;
var query = "SELECT * FROM Customer360 USE KEYS ($1)";
try {
var result = await bucket.query(query, [docId])
let rows = result.rows;
response.json(rows)
} catch (error) {
response.status(404).json({"error": "No match for " + error});
}
});
Hi Justin,
First of all, kudos for attempting to translate an older version of the course to SDK 3.0. I presume from your code that is the version you are using. We are working updating all the courses for the 6.5 version of the server and SDK 3.0. However, NodeJS probably won’t be updated for a while yet. In the mean time, here is the basic rule of thumb in SDK 3.0 for various tasks.
For KV operations (get, insert, upsert, delete, etc), you would work against the collection. For queries, you use the cluster. So, your query should read:
var result = await cluster.query(query, [docId])
1 Like
Thanks for the reply Mark.
I had actually tried the calling the query function from the cluster, but when I put that in my code I get Error: internal server failure
edit:
I can insert the get code from lab 3 and it works perfectly in that get function.
try {
var docId = request.params.id;
result = await collection.get(docId)
response.json(result.value)
} catch (error) {
response.status(404).json({"error": "No match for " + docId});
}
Hey @justinbkay,
Using the cluster object to execute queries with SDK 3.0 is indeed the right thing to do. I also noticed that your connection string for the cluster appears to be using a far older style which includes the management port. You should consider using the hosts style introduced in SDK 2.0 as the old style you’ve used was deprecated in SDK 2.0 and more or less removed from SDK 3.0:
var couchbase = require("couchbase");
var express = require("express");
var bodyParser = require('body-parser');
var port = process.env.port || 3000;
// Open bucket and create bucket reference for use in routes
var cluster = new couchbase.Cluster("couchbase://127.0.0.1", {
username: 'Customer360',
password: 'password'
})
var bucket = cluster.bucket("Customer360")
var collection = bucket.defaultCollection()
var app = express();
var jsonParser = bodyParser.json();
app.get("/country/:id", async function(request, response) {
var docId = request.params.id;
var query = "SELECT * FROM Customer360 USE KEYS ($1)";
try {
var result = await cluster.query(query, {
parameters: [docId]
})
let rows = result.rows;
response.json(rows)
} catch (error) {
response.status(404).json({"error": "No match for " + error});
}
});
Cheers, Brett
Thank you!
That got me working. I think it was probably the syntax for the query function that I was missing.