Async call causes identical code to execute fine in the console but not in a dataviewjs code block (solved) #1613
-
The problemWhen I copy and paste code from my codeblock into the console (after setting dv appropriately), I get different behaviors with identical code. In my code block I get an error
The error
In the Console I get a valid answer, no error
dv = app.plugins.plugins.dataview.api // first set dv so we can use the same code
DataviewApi {app: e, index: FullIndex, settings: {…}, verNum: '0.5.47', value: {…}, …}
p = dv.pages().where(file => file.meeting && file.meeting.type == "planning-tool").map(a => ['[[' + a.file.name + ']]', a.file.mtime])
var fileobj;
var fs = require('fs');
(await fs.stat('/Users/rcook/Library/Mobile Documents/iCloud~com~omnigroup~OmniGraffle/Documents/2021-2022 Timelines.graffle', (err, result) => { if (err) { console.log(err) } else { fileobj = result } } ))
p.values.push(['[2021-2022 Timelines.graffle](<file:///Users/rcook/Library/Mobile Documents/iCloud~com~omnigroup~OmniGraffle/Documents/2021-2022 Timelines.graffle>)', fileobj.mtime])
6
p[5]
(2) ['[2021-2022 Timelines.graffle](<file:///Users/rcook…niGraffle/Documents/2021-2022 Timelines.graffle>)', Mon Nov 28 2022 22:45:17 GMT+0700 (Indochina Time)] |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Throw in a few console logs to see which mtime we're erroring out on. |
Beta Was this translation helpful? Give feedback.
-
SolutionsSolution 1: put it in a handlerFound the problem: the await is async so fileobj is not set. Need to put all the things in the handler. Still have an issue formatting the date, but that seems solvable. (await fs.stat('/Users/rcook/Library/Mobile Documents/iCloud~com~omnigroup~OmniGraffle/Documents/2021-2022 Timelines.graffle', (err, result) => { if (err) { console.log(err) } else { p.values.push(['[2021-2022 Timelines.graffle](<file:///Users/rcook/Library/Mobile Documents/iCloud~com~omnigroup~OmniGraffle/Documents/2021-2022 Timelines.graffle>)', result.mtime]); dv.table(['file', 'mtime'], p) } } )) solution 2: use the synchronous version:The synchronous versions of NodeJS are under p = dv.pages().where(file => file.meeting && file.meeting.type == "planning-tool").map(a => ['[[' + a.file.name + ']]', a.file.mtime])
var fileobj;
var fs = require('fs');
fileobj = await fs.promises.stat('/Users/rcook/Library/Mobile Documents/iCloud~com~omnigroup~OmniGraffle/Documents/2021-2022 Timelines.graffle')
p.values.push(['[2021-2022 Timelines.graffle](<file:///Users/rcook/Library/Mobile Documents/iCloud~com~omnigroup~OmniGraffle/Documents/2021-2022 Timelines.graffle>)', fileobj.mtime.toString()])
dv.table(['file', 'mtime'], p) |
Beta Was this translation helpful? Give feedback.
Solutions
Solution 1: put it in a handler
Found the problem: the await is async so fileobj is not set. Need to put all the things in the handler. Still have an issue formatting the date, but that seems solvable.
Is there a synchronous version of
fs.stat()
I could use? Hmm...solution 2: use …