You can see what errors are produced in the console:
To check for errors, first open the developer tools by pressing Ctrl-Shift-I on Windows/Linux or Cmd-Opt-I on macOS, and then go to the āConsoleā tab.
It should have create the file in your vaults main directory.
this is used to make sure you are using the correct App instance as you could define another variable with the same name in your code. https://dmitripavlutin.com/javascript-scope/
your jsonContent should already show up in the console.
You are right. I wonder how I could miss that. I remembered I had checked it as well. I even searched for the whole computer. Anyway, I wonder why it decides that the current location is the main vault, not in the plugin directory, where the js file locates?
I see. So app is also a class as well. Reading the documentation I donāt see much explanation of it. Can you explain how it works?
Yes the content of that variable is displayed in the console, but I cannot call it again directly in it. Both jsonContent and this.app.plugins.plugins.dotmaker.jsonContent do not work.
I want to automatically update the output every time the plugin runs. create cannot overwrite the file, so I guess modify is the way to go. But I get:
Uncaught (in promise) TypeError: Cannot create property āsavingā on string āoutput.jsonā
The App class represents the currenlty running instance of obsidian.
This is because your jsonContent variable is only accesible inside your onload() function, to be able to read it you need to change its scope(see the link in my last reply).
The modify function requires the use of a TFile, you can get
this by using the getAbstractFileByPath function and the checking if the returned value is instanceof TFile.
If this is not the case you should create the file.
One thing I do notice is that you do not use TypeScript(which is the default for obsidian) to write your plugin, it should not even allowed you to compile the plugin.
I donāt know. Iām editing the sample plugin, so Iām actually coding with ts right now. npm can still compile it to js
However, itās interesting that when I use the path "/", I get a circular object:
JSON.stringify(this.app.vault.getAbstractFileByPath("/"))
VM314:1 Uncaught TypeError: Converting circular structure to JSON
--> starting at object with constructor 'e'
| property '_' -> object with constructor 'Object'
| property 'rename' -> object with constructor 'Array'
| index 0 -> object with constructor 'Object'
--- property 'e' closes the circle
at JSON.stringify (<anonymous>)
at <anonymous>:1:6
Add a console.log('captures/' + getFileName()) just to make sure your file name is correct. When I had this problem it turned out that I had a problem with the file path/name and node just wasnāt throwing me an error to explain.
getAbstractFileByPath always returned null if path included a hidden folder (e.g. : .obsidian). Not sure what I am doing wrong.
So I decided to use app.vault.adapter.exists instead.
To create and overwrite a file, I just use app.vault.adapter.write. To read it if it exists, app.vault.adapter.read.
Not sure if it is the proper way to read/write files inside an Obsidian vault, but it seems to work here (so far Iāve only been able to test on Windows, Android 12 and iPad).
Hmm, I use getAbstractFileByPath for a file that is outside the vault and a file inside that vault, and both are not contained in a hidden folder, and itās still null.
Here is how this works:
Relative path for existing file inside .obsidian
const configPath = this.app.vault.configDir + "/plugins/dotmaker/data.json";
app.vault.adapter.exists(configPath)
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: true
app.vault.adapter.read(configPath)
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: ""
app.vault.adapter.write(configPath)
[[PromiseState]]: "rejected"
[[PromiseResult]]: "TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
Absolute path for existing file inside .obsidian
fileinside = app.vault.adapter.basePath + "\\.obsidian\\plugins\\dotmaker\\data.json";
app.vault.adapter.exists(fileinside)
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: false
app.vault.adapter.read(fileinside)
[[PromiseState]]: "rejected"
[[PromiseResult]]: Error: "ENOENT: no such file or directory, open 'D:\Quįŗ£ Cįŗ§u\B Nį»i dung\Knowledge graphs\CĆ¢y vįŗ„n Äį»\D:\Quįŗ£ Cįŗ§u\B Nį»i dung\Knowledge graphs\CĆ¢y vįŗ„n Äį»\.obsidian\plugins\dotmaker\data.json'"
app.vault.adapter.write(fileinside)
[[PromiseState]]: "rejected"
[[PromiseResult]]: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
For the file with absolute path outside .obsidian but inside vault, the error is the same with the file inside .obsidian.
checking the properties of app.vault.adapter I see a writeFile() fsPromise, with the arguments like this:
I try app.vault.adapter.writeFile(configPath, output) and it doesnāt work, but the autocomplete suggests me to try app.vault.adapter.write(configPath, output). This time it works. I have no idea how to get to understand solution though