Querying stardog5 from JS

Hi All,

I recently migrated from stardog3 to stardog5 and I am having issues with all of my "update" functionality. My read queries are working and my update queries work if I execute them directly in my stardog5 console.

Here is what I am doing:

var agent = new http.Agent({ maxSockets: 15 }); 
var con = new stardog.Connection();
      con.setEndpoint(config.URL); // This is set to my stardog instance:  http://stardog5.mysite.com:5000
      con.setCredentials(config.user, config.pass); //username, pass
var queryString = updatePublicationsQueryFile.toString() // Where this is my query, that I have tested it works. 

 con.query({
                database: config.db,
                query:  queryString,
                agent: agent
            },
                function (results) {
                    console.log(results)
                });

This gets me the following error:

Cannot execute update query on read endpoint stardog

I tried different ways to create the endpoit:
http://stardog5.mysite.com:5000/DBNAME/UPDATE
http://stardog5.mysite.com:5000/UPDATE

but none of them worked.

Any help will be appreciated.

Tona

Tona,

It looks like you are using the legacy 0.x version of stardog.js. We have since put out a 1.0 release that was made to work with Stardog 5. Check it out here or here!

Hi Stephen,

Thanks for the reply. I updated to the Stardog 1 and followed the example but I am not getting anything back in my "read" query:

this.query = function (terms, callback) {
//Update to newest query
const { Connection, query } = require('stardog');
const conn = new Connection({
username: config.stardogUser,
password: config.stardogPass,
endpoint: config.stardogURL,
});
console.log("RAN")
fs.readFile(__dirname + '/queries/query.rq', function (err, queryFile) {
var queryString = queryFile.toString();
console.log(queryString)
query.execute(
conn,
config.stardogDB,
queryString, 'application/sparql-results+json', {
limit: 1000,
offset: 0,
}).then(({ results }) => {
console.log("Got here")
});
});

"RAN" is being displayed in the console but I do not get "GOT HERE"

My query is a SELECT.

The stardog log is not throwing an error.

Thanks again for the help!.

Tona

Here's something that I just happened to run into GitHub - andhikanugraha/starmutt: An enhanced wrapper for https://github.com/clarkparsia/stardog.js It's a little old but might be the source of some inspiration for anyone working on JavaScript.

What you have looks OK to me at first glance. Could be something with fs with which I’m unfamiliar. Is your console.log(queryString) line being printed to the console? If so, what if you add a catch to the end of the query.execute promise chain? Does that surface an error?

Hi Stephen,

Sorry for the slow response, yes the queryString is printed and it is populated correctly. I updated the code with your recommendation and I added a catch however it is being printed as undefined:

fs.readFile(__dirname + ‘/grants_queries/all_grants.rq’, function (err, allGrantQueryFile) {
var filters = utils.buildGrantFilters(terms);
var queryString = allGrantQueryFile.toString().replace("##ABOUT##", filters);
// console.log(queryString)
query.execute(
conn,
config.stardogDB,
queryString, ‘application/sparql-results+json’, {
limit: 1000,
offset: 0,
}).then(({ grant_results }) => {
console.log(“RAn2”)
callback(utils.transformGrantsToJSON(grant_results))
}).catch(({ error }) => {
console.log(“RAN THE CATCH”)
console.log(error)
});
});

catch accepts a function that accepts an Error object, so you don't want to destructure it. You should do simply

.catch(error => {
console.log(“RAN THE CATCH”)
console.log(error)
});

My bad =)

That prints the following:

{ FetchError: request to http://mypage.onbc.io:5820/DEV/query?limit=1000&offset=0 failed, reason: read ECONNRESET
at ClientRequest. (/project_files/midas/obc-api/node_modules/node-fetch/index.js:133:11)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:306:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1272:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'FetchError',
message: 'request to http://mypage.onbc.io:5820/DEV/query?limit=1000&offset=0 failed, reason: read ECONNRESET',
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET' }

With an ECONNRESET, is something appearing in stardog.log that might indicate why the connection is being rejected?

Hi Stephen,

I found out my issue was in the connection at my institution. I connected to a VPN and I was able to connect to the server.

I did change my receiving code to read at “body” instead of the variable I had.

Thanks for the help.

Tona

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.