Fix for error message – used prefix has not been declared

When developing with the Carbon LDP JavaScript SDK and using the sparqler library, you could stumble upon the error: Error: The used prefix has not been declared. In one case where I generated this error, the complete console error was as follows:

ExploreComponent.html:48 ERROR Error: The used prefix has not been declared
    at IRIResolver.push../node_modules/sparqler/iri/IRIResolver.js.IRIResolver.resolve (IRIResolver.js:23)
    at TriplesPattern.js:69
    at Array.reduce ()
    at Variable.push../node_modules/sparqler/patterns/triples/TriplesPattern.js.TriplesPattern._resolvePath (TriplesPattern.js:57)
    at Variable.push../node_modules/sparqler/patterns/triples/TriplesPattern.js.TriplesPattern._addPattern (TriplesPattern.js:33)
    at Variable.push../node_modules/sparqler/patterns/triples/TriplesPattern.js.TriplesPattern.has (TriplesPattern.js:14)
    at explore.service.ts:248
    at Array.forEach ()
    at ExploreService.push../src/app/services/explore.service.ts.ExploreService._constructFiltersPatterns (explore.service.ts:238)
    at explore.service.ts:115

The problem was with the following offending line in my code:

filterPatterns = _.var(subjectName).has(filter.property.id, _.literal(filter.object).ofType(filter.property.range));

The filter.property.id string was being interpreted as a prefix and the fix was to wrap filter.property.id with the _.resource() method of the PatternBuilder utility like this:

_.resource(filter.property.id)

This made it so that a string such as https://example.com/ would be reflected instead as <https://example.com/>.

General information about querying can be found in the Carbon LDP product documentation on Querying.