There are some caveats to this approach which has stopped you from getting it to run properly, so let us go through them one by one.
Joining the query parts
Working out of this example to get the different parts:
let upFileStr = await dv.io.load("upperFile.md");
let downFileStr = await dv.io.load("lowerFile.md");
let allFileStr = upFileStr + lowerFileStr;
When you join the two parts of the query, it’s not unlikely that you join them without any white space between them, and you get something like: ...WHERE things="1"WHERE color ...
.
To fix this, either use upFileStr + "\n" + lowerFileStr
, or be very sure that there is an extra return in the upper part file. Also be sure that there is no extra text or frontmatter in either file. This will be included in the joined text string.
A query needs to be awaited
You need to do await dv.query()
to allow the query to be executed properly. There is a variant dv.tryQuery()
which will fail miserably when wrong, which doesn’t need await
, but I find it better to use the first variant. (This is a matter of personal preference, though)
When you await such a query, you get a structured return, so if you do console.log(dv.query( ... ))
you’ll see something like this:
This shows us that we don’t actually get the finalised query, but we get an indicator to whether it went well or not in the successful
variable, and the actual result in the value
variable.
This comes in handy when we want to produce the final result.
Presenting the structured result
A dv.query()
needs to be done in combination with either dv.taskList()
, dv.list()
or dv.table()
, see Codeblock Reference - Dataview. And these require slightly different way of handling the result from the dv.query()
.
In your case you’re doing a TABLE
query, it seems, so you need to do something along these lines:
const result = await dv.query( ... )
if (result.successful)
dv.table(["Field A", "Field B", "Thing"], result.value.values)
else
dv.paragraph("~~~~\n" + result.error + "\n~~~~")
Here we first await the query, and store the result in result
, then we check if it was a successful run, and present the output through dv.table()
. Finally, if it failed we present the error message.
Fully working example of combining query parts with base data
To be able to test this I produce the following files, and test data:
upperPart.md
TABLE WITHOUT ID fieldA, fieldB, thing
FLATTEN things as thing
WHERE this.file.name = file.name
lowerPart.md
WHERE thing = 2
Combining the script parts
fieldA:: aValue
fieldB:: bValue
things:: 1, 2, 3, 4
```dataviewjs
const upperPart = await dv.io.load(dv.current().file.folder + "/upperPart.md")
const lowerPart = await dv.io.load(dv.current().file.folder + "/lowerPart.md")
// console.log(upperPart, lowerPart)
const result = await dv.query(upperPart + lowerPart)
// console.log(result)
if (result.successful)
dv.table(["Field A", "Field B", "Thing"], result.value.values)
else
dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```
In the example above I’ve compiled my own test suite to properly test this, and hopefully the text and the example will help you achieve your goal, and understand why your previous attempts have failed.