Adding variables to DQL

What I’m trying to do

I have a bunch of queries like this in my monthly journal post that searches through that month’s daily notes for a prayer list that contains a hashtag and combines them into one list. This makes all of my prayer requests viewable in one spot every month, which is nice.

```dataviewjs
const query = `TABLE C.text AS "Prayer - Supplication - Community"
FROM "Calendar/Journal/2023/02_Feb"
FLATTEN file.lists AS L
WHERE contains(L.tags, "#🙏/Supplication/Community")
FLATTEN L.children AS C`

// executing the dataview query and return results
let DQL = await dv.tryQuery(query);

// render the table only if any value
if (DQL.values.length > 0){
	dv.table(DQL.headers, DQL.values)
}
```

This works pretty well but I would like to pull it into its on js file called something like Query Prayer Requests.js so that in my monthly note I can just have a dv.view() like:

```dataviewjs
dv.view("Query Prayer Requests", {columText: "Prayer - Supplication - Community", searchTag: "#🙏/Supplication/Community"})
```

I am having trouble pulling the text into variables, as I am not sure how to get the query to recognize them.

I am also not sure how I am going to deal with the FROM "Calendar/Journal/2023/02_Feb" as this will change each month / year.

Things I have tried

Variables

First I pulled the strings into variables then I tried to put them into ${columText} and ${searchTag}. I saw somwhere the suggestion to use just the $columText and $searchTag. I have tried surrounding it with different quotes ` ’ " but I keep getting a syntax error.

Evaluation Error: SyntaxError: Unexpected identifier
    at DataviewInlineApi.eval (plugin:dataview:18370:21)
    at evalInContext (plugin:dataview:18371:7)
    at asyncEvalInContext (plugin:dataview:18378:16)
    at DataviewJSRenderer.render (plugin:dataview:18402:19)
    at DataviewRefreshableRenderer.maybeRefresh (plugin:dataview:17980:22)
    at HTMLDivElement.r (app://obsidian.md/enhance.js:1:11445)

Here is my current Query Prayer Requests.js file that is being pulled into the dv.view()

let {columText, searchTag} = input;
let directory = "Calendar/Journal/" + moment(dv.current().file.name, 'YYYY-[M]MM').format('YYYY/MM_MMM');
const query = `TABLE C.text AS "${columText}"
FROM ${directory}$
FLATTEN file.lists AS L
WHERE contains(L.tags, "${searchTag}$")
FLATTEN L.children AS C`

// executing the dataview query and return results
let DQL = await dv.tryQuery(query);

// render the table only if any value
if (DQL.values.length > 0){
	dv.table(DQL.headers, DQL.values)
}

Date

Once I get the variables situated and working, I am hoping that something like this might work for the dates.

let directory = "Calendar/Journal/" + moment(dv.current().file.name, 'YYYY-[M]MM').format('YYYY/MM_MMM')

So, I’m not getting the error message you’ve got, and I had to guess for the file structure, but I made up my own:

  • Folder: Calendar/Journal
    • file: 2023-M01 with the dv.view(filename, { columText: "hi", searchTag: "bye" }) call
    • folder: 2023/
      • file: 01_Jan with the following content:
  • #bye Hooray
    • test 1
    • test 2
  • #bye Congrats
    • test 3
    • test 3

And after the corrections listed below, I got this output:
image

Basically, there was two errors in your script:

  • The syntax for FROM with folders/files, are FROM " ... ", you left out the quotes, so you’ll need to change that line to FROM "${ directory }"
  • When using template literals, you shouldn’t have a $ at the end. This is wrong: ${ searchTag }$, and this is the correct variant: ${ searchTag } (I like to keep extra spaces at front and end within the curly braces, they get deleted anyways, and they make the statement a little easier to read)

To actually locate these errors, I used Developers Tools and looked at the Console pane, after sprinkling various statements like console.log(input), console.log(directory, columText, searchTag), and finally console.log(query) and console.log(DQL) after the setting of each of these variables. This allows me to see the various variables, and that they actually contain what they’re supposed to contain.

So try correcting those two errors, that is add double quotes in the FROM statement, and loose the extraneous $ in all three references at the end, then see what happens.

Regarding the date thingy, if your directory structure is like I depicted above, it should be working just nicely.

Hmm… well that sucks, just seems to be a typo here and there. I also found a quote that was the fancy double quote instead of the programming straight quote that always messes thing sup. On the plus side, I just found out that Obsidian has a developer console…

Thanks again @holroy

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