Image link in note gives error in for loop of DataviewJS. How to fix this?

What I’m trying to do

I am running a dataviewjs code that displays all the tasks in a note (“noteA”) and all the notes that are linked to it (“noteA”). This is the code I am running:

```dataviewjs
const first = dv.current().file.name;
dv.taskList(dv.page(first).file.tasks
.where(t => !t.completed));

const notes = dv.current().file.outlinks;
for (let note of notes) {
	if(dv.page(note).file.name != first){
		let ntask = dv.page(note).file.tasks.where(t => !t.completed);
		if(ntask.length > 0) {
			dv.taskList(ntask);
		}
	}
}
```

This works but if I insert a image/picture in the note (“noteA”), the dataviewjs code breaks with error showing:

Evaluation Error: TypeError: Cannot read properties of undefined (reading 'file')

How can I tell the code to avoid image links in the for loop?

Found the answer thanks to AB1908 reply here.

I had to edit one line by adding .where(x=> !x.path.endswith("png") to not take links that end with “png”.

So I changed this line:

const notes = dv.current().file.outlinks;

to this:

const notes = dv.current().file.outlinks.where(x => !x.path.endsWith("png"));

so now the whole code looks like:

```dataviewjs
const first = dv.current().file.name;
dv.taskList(dv.page(first).file.tasks
.where(t => !t.completed));

const notes = dv.current().file.outlinks.where(x => !x.path.endsWith("png"));
for (let note of notes) {
	if(dv.page(note).file.name != first){
		let ntask = dv.page(note).file.tasks.where(t => !t.completed);
		if(ntask.length > 0) {
			dv.taskList(ntask);
		}
	}
}
```

MAJOR UPDATE:

Using const will make the variable constant. And so if you change the name of the note, then you will always have to reload the dataviewjs for the code to work. So, change all const to let.

The code looks like this now:

```dataviewjs
let first = dv.current().file.name;
let firstTask = dv.current().file.tasks.where(t => !t.completed)
if(firstTask.length > 0) {
	dv.taskList(firstTask);
}


let notes = dv.current().file.outlinks.where(x => !x.path.endsWith("png"));
for (let note of notes) {
	if(dv.page(note).file.name != first){
		let ntask = dv.page(note).file.tasks.where(t => !t.completed);
		if(ntask.length > 0) {
			dv.taskList(ntask);
		}
	}
}
```

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