Help with a dataviewJS query pulling from an array

Things I have tried

i am using this array to pull data into a couple different query’s at the moment. my heatmap calendar was working untill i decided to move mood into the array.

The array:

wellbeing:
  mood: 2
  mood-notes: terrible
  Motivation: 4
  motivation-notes: Had a lot to get finished
  health: 3
  health-notes: covid
  pain: 4
  pain-type: back and neck pain

“mood” used to be on its own in my YAML and the query worked just fine. not sure how i can get this working.


The query i had working before i added “mood” to the array under “wellbeing”:

dataviewjs
width: 100%
dv.span("")
const hue1 = 13 //red
const hue2 = 132 //green
const calendarData = { 
    intensityScaleStart: 1,
    intensityScaleEnd: 10,
    colors: {   // optional, defaults to green
        red2greenX21: [
            `hsl(${hue1}, 100%, 37%)`,     // 1 - darkest red (worst mood)
            `hsl(${hue1}, 100%, 50%)`,     // 2 - 
            `hsl(${hue1}, 100%, 60%)`,     // 3 - 
            `hsl(${hue1}, 100%, 77%)`,     // 4 - lightest red
            `hsl(0, 0%, 80%)`,             // 5 - gray (neutral mood)
            `hsl(${hue2}, 59%, 24%)`,      // 6 - lightest green
            `hsl(${hue2}, 49%, 36%)`,      // 7 - 
            `hsl(${hue2*0.85}, 43%, 56%)`, // 8 - 
            `hsl(${hue2*0.7}, 70%, 72%)`,  // 9 - dark green
            `hsl(${hue2*0.7}, 80%, 82%)`,  // 10 - darkest green (best mood)            
        ],
    },
    entries: [] // populated in the DataviewJS loop below
}
for(let page of dv.pages('"1. Notes/0. dailyNotes"').where(p=>p.wellbeing.mood)){ 
    calendarData.entries.push({
        date: page.file.name, 
        intensity: page.wellbeing.mood,
        content: await dv.span(`[](${page.file.name})`), //for hover preview
    })
}
renderHeatmapCalendar(this.container, calendarData)

The query when it was working and mood was on its own in the YAML

dataviewjs
width: 100%
dv.span("")
const hue1 = 13 //red
const hue2 = 132 //green
const calendarData = { 
    intensityScaleStart: 1,
    intensityScaleEnd: 9,
    colors: {   // optional, defaults to green
        red2greenX21: [
            `hsl(${hue1}, 100%, 37%)`,     // 1 - darkest red (worst mood)
            `hsl(${hue1}, 100%, 50%)`,     // 2 - 
            `hsl(${hue1}, 100%, 60%)`,     // 3 - 
            `hsl(${hue1}, 100%, 77%)`,     // 4 - lightest red
            `hsl(0, 0%, 80%)`,             // 5 - gray (neutral mood)
            `hsl(${hue2}, 59%, 24%)`,      // 6 - lightest green
            `hsl(${hue2}, 49%, 36%)`,      // 7 - 
            `hsl(${hue2*0.85}, 43%, 56%)`, // 8 - 
            `hsl(${hue2*0.7}, 70%, 72%)`,  // 9 - darkest green (best mood)
        ],
    },
    entries: [] // populated in the DataviewJS loop below
}
for(let page of dv.pages('"1. Notes/0. dailyNotes"').where(p=>p.mood)){ 
    calendarData.entries.push({
        date: page.file.name, 
        intensity: page.mood,
        content: await dv.span(`[](${page.file.name})`), //for hover preview
    })
}
renderHeatmapCalendar(this.container, calendarData)

and the error im now getting :
image

VS. What it should look like.
image

Again, Any help would be greatly appreciated.

I think it could as simple as doing p.wellbeing?.mood instead of p.wellbeing.mood. The point is that in your query you also look into pages not having the wellbeing field set, and when it then tries to see the sub-field of mood in that undefined field, it crashes.

When using p.wellbeing?.mood you’re saying that if wellbeing is defined, then look for a potential mood field. If it doesn’t exist, it bails out gracefully, just like the previous use of p.mood, which also would bail out if that wasn’t set.

So every time you introduce multiple levels of fields/object references like this, try adding the question mark to allow for it to be non-existent.

That was totally it. lol to think it was just missing a ? XD. ty ty. that was bothering me so much this morning lol

1 Like

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