RubenVerborgh / SPARQL.js

A parser for the SPARQL query language in JavaScript
Other
342 stars 66 forks source link

SPARQL Generator error "Cannot read property 'termType' of undefined" #140

Closed alexander-schulze closed 3 years ago

alexander-schulze commented 3 years ago

Dear RubenVerborgh, when running the code I get the following error "Uncaught TypeError: Cannot read property 'termType' of undefined", while trying to re-generate the SPARQL. Is this a known issue and how could I fix it? The parser works, but the generator fires this issue. I'm using VSC 1.54.3 with node 12.20 on a macOS 10.13.6 (High Sierra). Thanks in advance for looking at that and giving a hint. Best Regards, Alex

RubenVerborgh commented 3 years ago

Do you have the steps to reproduce?

alexander-schulze commented 3 years ago

I ran the following steps:

{
  "name": "sparqljs-test",
  "version": "1.0.0",
  "description": "SPARQLJS Test",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/innotrade/sparqljs-test.git"
  },
  "keywords": [
    "SPARQL",
    "JS"
  ],
  "author": "Alexander Schulze",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/innotrade/sparqljs-test/issues"
  },
  "homepage": "https://github.com/innotrade/sparqljs-test#readme",
  "dependencies": {
    "sparqljs": "^3.4.1"
  }
}
/Users/alexanderschulze/.nvm/versions/node/v12.20.0/bin/node ./index.js
Process exited with code 1
Uncaught TypeError: Cannot read property 'termType' of undefined

The parser works fine, I can see the resulting SPARQL AST object. The generator.stringify(parsedQuery); raises the above exception.

var generator = new SparqlGenerator({
  /* prefixes, baseIRI, factory, sparqlStar */
});
parsedQuery.variables = ["?mickey"];
var generatedQuery = generator.stringify(parsedQuery);

Thanks in advance for your support!

alexander-schulze commented 3 years ago

My launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "runtimeVersion": "12.20.0",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}/index.js"
    }
  ]
}
alexander-schulze commented 3 years ago

Missed to say thanks for the quick response. Just granted you access to @innotrade/sparqljs-test.

alexander-schulze commented 3 years ago

Debugged it and found it, the parsedQuery.variables = ['?mickey']; is causing the error. I will play more with it and give you feedback. Would be interested in a cooperation on that one.

AlexeyMz commented 3 years ago

@alexander-schulze SPARQL.js uses RDF/JS spec to represent RDF terms including variables, so you need to use RDF term factory to create variable in this case:

import { DataFactory } from 'rdf-data-factory';
var generator = new SparqlGenerator({
  /* prefixes, baseIRI, factory, sparqlStar */
});
var dataFactory = new DataFactory();
parsedQuery.variables = [dataFactory.variable("mickey")];
var generatedQuery = generator.stringify(parsedQuery);