Issuing creating dataviewJS table

I am trying to create a variant of a dataviewjs script from the Dataview Example Vault.

This is my code


```dataviewjs

const startDate = date(now);
const endDate = date(now) - dur(7 d);

pages = dv.pages("#dailyNote").where(p => p.file.cday <= startDate && p.file.cday >= endDate);

dv.table(["Day", "Morning", "Afternoon", "Evening", "Average"], pages.map(p => [p.file.link, Morning, Afternoon, Evening, 0]));

```

No matter what I attempt to do I get an SyntaxError: missing ) after argument list error. Since all of the brackets are balanced (and it doesn’t matter what I take out of the code) I have to assume that I am using the .pages or .table functions incorrectly.

What have I done incorrectly here? Probably something obvious :slight_smile:

Also, how is one meant to debug this types of code?

TIA

I’m not able to debug it properly just now, but I do believe you need to do p.Morning and so on in the map for starters.

I did have it earlier on a different version of the code. I’ve added them back and it hasn’t fixed the issue.

Initially it was the .where clause that was causing the problem but now it still generates an error with out it.

Thanks

What are you trying to do with this query?

You’re mixing syntax from DQL queries with dataviewjs. So there are multiple errors in your script.

You need to write proper javascript, for this to function. Give me a few minutes, and I’ll see what I can do with it…

So here is a version which don’t have any syntax errors, but it’s not producing any result since there are no notes tagged with “#dailyNote”.

```dataviewjs

const startDate = dv.func.date("now");
const endDate = dv.func.date("now") - dv.func.dur("7 days");
console.log("StartDate: ", startDate)
console.log("EndDate: ", endDate)

const pages = dv.pages("#dailyNote")
   .where(p => p.file.cday <= startDate && p.file.cday >= endDate);

dv.table(
  ["Day", "Morning", "Afternoon", "Evening", "Average"],
  pages.map(p => [p.file.link, p.Morning, p.Afternoon, p.Evening, 0]));

```

The endDate is still not correct, as this is not a common way to work with the dates within dataviewjs.

Some of the errors I’ve fixed already:

  • You need to do dv.func.date(), and not just date(), if you want to use the DQL date function
  • You need to add quotes around "now" and "7 d" to have legal syntax
  • You need to declare the pages variable, like const pages = ...
  • You do need to use the p.Morning & co, as I said earlier

Now over to the logical issues:

  • These changes made it compile, but there are no notes tagged #dailyNote within my version of the dataview example vault, so it still returns nothing
  • There is something funky with the date - dur in this version, so the endDate is still just the millisecond, and not a date

To debug, use console.log(...), and open up the Developer Tools on the Console pane.

Do you know any javascript? Some of these errors are somewhat basic stuff…

1 Like

If I were to write something similar, I don’t think I would use dv.func.date() and dv.func.dur() at all, but rather use something from the moment library. But I see that in the dataview example vault, they’re using some of the dv.luxon stuff. For an example look into, 20 Dataview queries/Calculate cycle lengths and durations.

But I suspect you don’t really now too much javascript, so it might be a good idea to actually learn javascript before venturing on with stuff like this. Date manipulation can be a little tricky, to say the least, so when you don’t know the difference between DQL queries and javascript, you’re in for a lot of surprises and mystery if you just copy-paste code.

I went a different route,

const startDate = DateTime.now;
const endDate = DateTime.now().plus({ days: 7 });

dv.table(["Day", "Morning", "Afternoon", "Evening", "Average"], dv.pages("#dailyNote").where(p => p.file.cday <= startDate && p.file.cday >= endDate)
.map(p => [p.file.link, p.Morning, p.Afternoon, p.Evening, 0]));

I believe the addition of console.log(...) for debugging is a great idea though

So it’d be,

const startDate = DateTime.now;
const endDate = DateTime.now().plus({ days: 7 });
console.log("StartDate: ", startDate)
console.log("EndDate: ", endDate)

dv.table(["Day", "Morning", "Afternoon", "Evening", "Average"], dv.pages("#dailyNote").where(p => p.file.cday <= startDate && p.file.cday >= endDate)
.map(p => [p.file.link, p.Morning, p.Afternoon, p.Evening, 0]));

This is why its better to use Luxon instead of DQL formatting.

In addition, I’m guessing he meant plus instead of minus. If something is starting now, it cannot end 7 days ago.

Well…that’s a bit unfair…most of his errors involved DataviewJS specifically. When going from Javascript to DataviewJS, there’s many learning curves involving the DataviewJS API

I have to also disagree with the learning Javascript before experimenting with DataviewJS. You should learn basic syntax but, you gotta fuck around and experiment, you know? Most programmers learn that way. DQL is more limited than DataviewJS, so its more appetizing

You did a great job explaining everything though. Keep up the good work!

Sorry if it was offending, that wasn’t my intent. But not knowing the difference between variable references like now versus "now", and blindly referencing DQL stuff within javascript, is kind of a red flag to me.

Then again, it’s late in the night (or getting to be early in the morning), so maybe I should refrain from answering at all at this time of night. Wasn’t trying to be unfair, and apologise if it seemed to be that.

And I’m all in favour for experimenting and trying stuff out, but one do need to have some basic knowledge of the language to be able to get somewhere, and date manipulation in combination with predicate functions might not be the best place to start, in my opinion.

Anyways, I’m off to bed.

Well sorry for that.

Thanks for your help.

And yet you have pissed me off enough that I’m not coming back.

Thanks for the solve. I wasn’t actually looking for total rewrite, just a pointer to what I was messing up. Guess it must be “shit on the new guy” day.

I’l let myself out.

I don’t see why it wouldn’t be. As you can see, the fix was pretty simple.

Date manipulation is pretty straightforward when using Luxon

That’s an easily looked over syntax error…happens all the time…

DataviewJS is an API and DQL is very similar to it. You cannot expect someone barely looking into DataviewJS to understand all of its nuances

I know you didn’t mean any harm. But, definitely came off quite condescending. That last line, “Do you know any javascript? Some of these errors are somewhat basic stuff…” was very pointed and unnecessary. You could’ve left it out.

Imagine if in my post I said, “How could you not know to use Luxon instead of dv.func?..thats pretty basic knowledge…”

I have Autism. My communication skills are sometimes dog shit and I understand accidentally hurting others all too well. All of my social skills are learned. You likely didn’t mean it in that way and I’m sure you didn’t think about it before writing that.

Obviously you had good intentions, you’re helping out in your free time! You could have been doing anything else. Nonetheless, please think things over before saying them.

I know all too well how vulnerable asking for help is and its not easy. You don’t have to get all buddy-buddy with everyone but, being more gentle is the least you can do

You gotta think, is what I’m saying necessary? Will it be helpful? Saying his mistakes were basic knowledge was belittling and very unnecessary

I’m not the one that you should apologize to, I was simply pointing things out

Out of curiosity, if I was going to learn JavaScript with the intention of quickly getting up to speed with Dataviewjs, are there any specific topics that must not be skipped? Also, are there any topics that could be skipped over?

I was beginning to check out a free course on Codeacademy for JavaScript, but never really got started. I would be open to buying a course if it came highly recommended. Any suggestions? This is just kind of a side thing I would like to learn slowly in bits and pieces.

Thanks!

If you’re learning JavaScript exclusively for DataviewJS, I’d recommend beginning with how Javascript works and basic Javascript syntax

Having an idea of what a programming language means is pretty important for understanding the code. You don’t need to go in depth. The following video should help, JavaScript: How It's Made - YouTube

As for syntax,

As for a “programmer mindset,”

You can skip learning how to run your code outside of Obsidian. You don’t need to know the HTML, CSS, and JS setup as its all built in. No need for Node.js either

Afterwards, It should be query methods

This video is quite helpful for beginning, The JavaScript Survival Guide - YouTube

Next, asynchronous javascript and Chrome Dev Tools(for code debugging)

Make sure to dive into Luxon

The most important is learning DataviewJS


Overall, I think its very important to just try things out yourself! Getting an error and learning how to resolve it is a huge step in the process

Take advantage of examples in the documentation, showcase, and the dataview community. You don’t have to start from scratch

Most elements of Javascript and DataviewJS can be learnt on the way! Feel free to message me if you ever need help with anything

My advice isn’t perfect, of course. I should make a proper roadmap for learning DataviewJS and I’ll try to put that in my schedule

2 Likes

Thank you very much! Perfect!

1 Like

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