Why does dataviewjs cannot get property of a link when it's inside an array of links?

If I have:

const inlinks = dv.current().file.inlinks
console.log(inlinks)
for (const i of inlinks) {
    result.push(i.path) 
} 
dv.span(result)
console.log(result)

Then it works fine. But if I convert inlinks to another array of links:

const inlinks = dv.current().file.inlinks
const linkList = []
for (const inlink of inlinks) {
    const data = dv.page(inlink)["Khái niệm"]
    if (Array.isArray(data)) {
        linkList.push(...data)
    } else {
        linkList.push(data)
    } 
}
const result = [] 
for (const i of linkList) {
    result.push(i.path) 
} 
dv.span(result)
console.log(result)

Then the console doesn’t return anything, and the render says Cannot read properties of undefined (reading 'path'). But if I console.log(result) inside the second loop, it still works fine. What happens?

Most likely you’ve got some issue with this statement: dv.page(inlink)["Khái niệm"] what are you expecting it to return, and what does ut actually return? Try using some log statements around this.

If you can’t figure out of by doing that, please provide some examples on what your inlinks property contains in your source files. What do you do if data is null?

Say I have several regular notes with field “Concept” like this:

# Note A
Concept:: [[1]] 

# Note B
Concept:: [[1]], [[2]] 

# Note C
Concept:: [[1]], [[3]], [[4]]  

At [[1]] I have that script, and want it to list other concepts that are attached to notes linking to it. The result should be [[2]], [[3]], [[4]].

  • inlinks is a proxy whose value is [[A]], [[B]], [[C]]
  • linkList is an array whose value is [ [[1]], [[2]], [[3]], [[4]] ]

If I console.log(result) inside the second loop, in each iteration it returns the array of paths just fine. But outside the loop it doesn’t return anything.

If data is null then it won’t pass the if (Array.isArray(data)) condition, so it won’t be added to linkList.

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