# Querying stardog5 from JS

**URL:** <https://community.stardog.com/t/querying-stardog5-from-js/1099>\
**Category:** Support\
**Created:** [June 21, 2018, 5:47pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099 "2018-06-21T17:47:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tvmendoza](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tvmendoza](https://community.stardog.com/u/tvmendoza)\
**Post date:** [June 21, 2018, 5:47pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/1 "2018-06-21T17:47:01Z")

</div>

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/DBNAME/UPDATE)  
[http://stardog5.mysite.com:5000/UPDATE](http://stardog5.mysite.com:5000/UPDATE)

but none of them worked.

Any help will be appreciated.

Tona

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [June 22, 2018, 11:50am UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/2 "2018-06-22T11:50:06Z")

</div>

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](https://github.com/stardog-union/stardog.js) or [here](https://www.npmjs.com/package/stardog)!

---

<div class="post-metadata">

**Author:** ![tvmendoza](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tvmendoza](https://community.stardog.com/u/tvmendoza)\
**Post date:** [June 22, 2018, 2:43pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/3 "2018-06-22T14:43:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [June 24, 2018, 3:10pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/4 "2018-06-24T15:10:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [June 27, 2018, 4:05pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/5 "2018-06-27T16:05:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tvmendoza](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tvmendoza](https://community.stardog.com/u/tvmendoza)\
**Post date:** [June 28, 2018, 7:53pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/6 "2018-06-28T19:53:24Z")

</div>

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)  
});  
});

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [June 28, 2018, 7:58pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/7 "2018-06-28T19:58:59Z")

</div>

> [@tvmendoza](#):
>
> .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

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

```

---

<div class="post-metadata">

**Author:** ![tvmendoza](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tvmendoza](https://community.stardog.com/u/tvmendoza)\
**Post date:** [June 28, 2018, 8:16pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/8 "2018-06-28T20:16:50Z")

</div>

> [@stephen](#):
>
> .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](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](http://mypage.onbc.io:5820/DEV/query?limit=1000&offset=0) failed, reason: read ECONNRESET',  
type: 'system',  
errno: 'ECONNRESET',  
code: 'ECONNRESET' }

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [July 5, 2018, 2:49pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/9 "2018-07-05T14:49:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tvmendoza](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tvmendoza](https://community.stardog.com/u/tvmendoza)\
**Post date:** [July 5, 2018, 4:54pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/10 "2018-07-05T16:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [July 19, 2018, 4:54pm UTC](https://community.stardog.com/t/querying-stardog5-from-js/1099/11 "2018-07-19T16:54:42Z")

</div>

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