Tuesday, May 31, 2016

Doing sparql queries with JavaScript

 I tried this:

var $rdf = require('../rdflib.js');

var store = $rdf.graph()
var timeout = 5000 // 5000 ms timeout
var fetcher = new $rdf.Fetcher(store, timeout)
var url = 'http://bshambaugh.rww.io';
var LDP = $rdf.Namespace('http://www.w3.org/ns/ldp#');

fetcher.nowOrWhenFetched(url, function(ok, body, xhr) {
    if (!ok) {
        console.log("Oops, something happened and couldn't fetch data");
    } else {

   var me = $rdf.sym('http://bshambaugh.rww.io');
   var contains = LDP('contains')

   var duh = $rdf.queryToSPARQL('SELECT *  FROM <http://bshambaugh.rww.io> WHERE { ?s ?p ?o . }');

   console.log("I fetched the data");
But I am getting issues like:

NamedNode { uri: 'http://www.w3.org/ns/ldp#contains' }
    for (var i = 0; i < query.vars.length; i++) {

TypeError: Cannot read property 'length' of undefined
    at getSelect (/home/brent/rdflib.js/dist/rdflib-node.js:6276:35)
    at getSPARQL (/home/brent/rdflib.js/dist/rdflib-node.js:6338:12)
    at Object.$rdf.queryToSPARQL (/home/brent/rdflib.js/dist/rdflib-node.js:6341:10)
    at /home/brent/rdflibexperiments/exp7.js:18:19
    at doneFetch (/home/brent/rdflib.js/dist/rdflib-node.js:9783:52)
    at /home/brent/rdflib.js/dist/rdflib-node.js:10479:20
    at xhr.handle (/home/brent/rdflib.js/dist/rdflib-node.js:9365:9)
    at .onreadystatechange (/home/brent/rdflib.js/dist/rdflib-node.js:10478:19)
    at dispatchEvent (/home/brent/rdflib.js/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:591:25)
    at setState (/home/brent/rdflib.js/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:610:14)

I am not really sure what I am supposed to call for rdflib.js for a sparql query. Are there any other javascript libraries that allow me to do sparql queries?

I found https://github.com/antoniogarrote/rdfstore-js .

1 comment:

  1. it seems like your body content is empty.

    Recheck your fetcher method signature, I assume that it is something like

    fetcher.nowOrWhenFetched(url, undefined, function(success, errorMsg, xhr) { } );

    Otherwise, your target url is probably not correctly recognized as containing RDF/XML content

    Best regards