How do I iterate over the result of a query (store.any)?

I’m working through rdflib: Introduction for building web apps

this part of the code is modified cause forEach didn’t work.
I got

fetcher.load(folder).then( () => {
	let files = store.any(folder, LDP('contains'));
	for (var file of files) { console.log(' contains ' + file); }

and it says files is not iterable.
How do I access the results of store.any ?

Looks like a typo in the intro to me.
If you print out files, I expect it’s only a single file.
any is a convenience wrapper around anyStatementMatching and returns the first statement that matches the specified pattern

It should be store.each

You need to use match or each when you expect mutiple values. Store.any() is only for single value returns and can not be iterated.

thanx. so store.each instead of files.forEach… I’ ll try that or store.match when I come home…

No, not instead of, in addition to. like this:

fetcher.load(folder).then( () => {
	let files = store.each(folder, LDP('contains'));
	for (var file of files) { console.log(' contains ' + file.value); }

now I’ve tried

const $rdf = require('rdflib');                                                 
const {SolidNodeClient} = require("solid-node-client");                         
const client = new SolidNodeClient();                                           
const store = $rdf.graph();                                                     
const fetcher = $rdf.fetcher(store,{fetch:client.fetch.bind(client)});

const LDP = $rdf.Namespace('>');

let folder = $rdf.sym('');  // NOTE: Ends in a slash

fetcher.load(folder).then( () => {
	let files = store.match(folder, LDP('contains'));
	for (var file in files) { console.log(' contains ' + file.value); }

works neither with of neither with in neither with each neither with match neither with file neither with file.value.
but runs without errors, just gives no output…

  1. There is an extra > in your LDP namespace, remove it
  2. You need var file of files) not in files
  3. Either use store.each, or look for the object in the return for match().

These should both work :

const $rdf = global.$rdf = require('rdflib');                                   
const client = new (require("solid-node-client").SolidNodeClient)();            
const store = $rdf.graph();                                                     
const fetcher = $rdf.fetcher(store,{fetch:client.fetch.bind(client)});          
const LDP = $rdf.Namespace('');                        
const folder = $rdf.sym("");      
fetcher.load(folder).then( () => {                                              
  let files = store.each( folder, LDP('contains'));                             
  for (var file of files) {                                                     
    console.log('contains : ',file.value);                                         
  let statements = store.match( folder, LDP('contains'));                       
  for (var s of statements) {                                                   
    console.log('contains : ',s.object.value);                                     

The details, Jeff, the details. you’ ve got an eagle’s eye. both versions working :wink: