Uncaught promise error every time i create a note with a plugin that i am developing for personal use

i keep getting this uncaught promise error every time I create a new note . At first I didn’t know that this function would send back a promise, so I changed the code to account for that but the errors I got went from 72 to 70.This is very confusing as I
1.
Know the folder I want to create the note in already exist
2.
The note that should be created does not exist yet
3.
I got the path to the folder

So, in theory, this should work, but I keep getting this error Uncaught (in promise) Error: File already exists.
Here is the code:


import {Plugin, TAbstractFile } from 'obsidian'
export default class Name extends Plugin {
	AllFiles: TFolder[]
	AllFolders: TFolder[]
	Ribbon: HTMLElement;
	onload() {
		this.AllFolders = this.app.vault.getAllFolders();
		this.AllFiles = this.app.vault.getFiles();
		this.Ribbon = this.addRibbonIcon('dice', 'Autolink', (evt: MouseEvent) => {
			console.log("You have pressed the button") //works
			this.Autolink()

		});
		
		this.app.workspace.on('editor-change', (editor) => { //Works
			this.AllFolders = this.app.vault.getAllFolders();
			this.AllFiles = this.app.vault.getFiles();
			console.log("The editor has been changed")
		});
	}

	private async Autolink() {
		const file = this.app.workspace.getActiveFile()
		if (file) {
			const content = await this.app.vault.read(file)
			this.Splitlines(content);
			console.log("found the activefile")
		}
	}

	private Splitlines(Filecontent?: string) {
		const count = Filecontent ? Filecontent.split(/\r\n|\r|\n/) : null;
		if (count) {
			this.TheActualWork(count)
			console.log("Splitting the file content")
		}
	}
	private TheActualWork(stringarray: string[]) { //warning
		
		const Check="#"
		let temp: string
		let folder = ""
		let folderpath =""
		for (let i = 0; i < stringarray.length; i++) {

			temp = stringarray[i];

			//Check if the first letter of the string matches the symbol #
			if (temp.startsWith(Check)) {
				console.log("the textstarts with #") //works
				temp = temp.slice(3);

				const sliced = temp.replace(/ /g, '') 
				//if the letter matches, check if there is a folder that exists with that name get its path so that a note can be created in the folder
				for (let z = 0; i < this.AllFolders.length; z++) {
					console.log(this.AllFolders[z].name + " is the folder getting cheked and the lenght is " + this.AllFolders[z].name.length)
					if (this.AllFolders[z].name == sliced) {
						folder = sliced
						folderpath = this.AllFolders[z].path
						console.log("The text matches a current existing folder")
						console.log("The path is " + folderpath)
						break;
					} 
					//if no folder with that name is found the folder is set to a empty string
					else {
						folder = ""
						console.log("the folder " + sliced + " does not exist and its lenght is " + sliced.length)
						
						
					}
					
				}
				
			}
			//if this string does not start with the symbol #,check if there is a folder path
			else if (folder !== "") {
				console.log("the text does not start with #")
				let exist = false;
				//Remove the [[]]  
				let chararray = temp.split('')
				chararray = chararray.filter((ca) => ca != "[") //Warning
				chararray = chararray.filter((ca) => ca != "]")
				temp = ""
				for (let f = 0; f < chararray.length; f++) {
					temp += chararray[f]
				}
				//check if there is a note with that name
				for (let z = 0; z < this.AllFiles.length; z++) {
					if (temp == this.AllFiles[z].name) {
						exist = true;
						console.log("This file already exists")
					}
				}
				if (exist == false) {

					try {
						this.Createnote(folderpath, temp)
						console.log("this file doesnt exist so it is getting created")
					}
					catch (e) {
						console.log(e)
					}
					
				}
				//incase it doesnt exist create one and send it to the folder path found
				
				
			}
		}
	}
	private async Createnote(folderpath: string, Name: string) {
		await this.app.vault.create(folderpath, Name);
		return null
	}
}
export class TFolder extends TAbstractFile {

}

Ps:I am a complete newbie with javascript, I have not touched this language until know so any criticism regarding my code is appreciated

You used this.app.vault.create() incorrectly

See the definition

Thank you

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