File-system: where is the "documents" folder?


#1

Hello,

Another frustrating day in town…
I’m trying to read a data file I have created, using the file-system. Most, if not all, of the examples and code samples refer to the mysterious “documents” folder where you store the data. Eg fs.knownFolders.documents().getFile(FILE_NAME).

However, all of them fails to mention WHERE is the documents folder located in the local project directory? Is this a folder I create? Or how does my data file gets into the “documents” folder?

I have created a “documents” folder (tried lower case and capitalized) in the root of my project (aka “app”) directory. I’ve also tried to put it inside the “App_Resources” directory. None of the cases it can find a file inside the “documents” folder when I use fs.knownFolders.documents().getFile(FILE_NAME).

Anyone could share where is the “documents” folder should be located/created/used/etc? Thanks!


How to view .pdf file inside a folder in app using file system
#2

Did you take a look here https://docs.nativescript.org/cookbook/file-system
it shows you how to specify the path for the file in multiple different ways

and the best way to store the files is in a folder in your app directory


#3

I think most of my frustration comes from the fact, that yes, documentation is the first thing I read, but in some cases the information is inadequate or not present.

For instance, in this case, if you read the page you sent a link to (which I read quite a few times by now), it doesn’t say ANYTHING about WHERE in your local project directory you place the DOCUMENTS folder. It just keeps saying over and over var documents = fs.knownFolders.documents(); but that’s it. There’s no mention WHERE is the DOCUMENTS folder. Where should I create it?


#4

I hear ya, but this is because NativeScript is flexible so multiple options are available at your disposal.

Search knownFolders keyword in this forum and you will get ideas how people use file-system module to create/get folders and files


#5

Feel free to contribute to the documentation if you feel like it’s lacking.


#6

Sure thing! As soon as I become at least half-expert in {N} I will!

But I would like to re-iterate on my problem and how I finally solved it.

I had my “Documents” folder inside my local project’s “app” directory, and I had the data.json file inside the “Documents” folder as such:

-app
|-App_Resources
|-Documents
|-|-data.json

All the sample code I was looking at were using the “Documents” folder as an example, like so:
let file = fs.knownFolders.documents().getFile("data.json");
but this didn’t work. It didn’t find the file.

I also tried:
let file = fs.knownFolders.documents().getFile("./data.json");
Same thing. Doesn’t work.

Finally, by some accident, I switched out the documents() to currentApp() like so:
let file = fs.knownFolders.currentApp().getFile("./Documents/data.json");

To this day I don’t know why using the document() call does not worked. I did console.log the values, but they only kept giving me [object Object] (which is a known bug), so that didn’t help.

I also could not use the debugger (in VSCode) because for some reason, at app startup, the debugger never kicked in when I tried to read the file. It did the logs, so it did go through the code, it just never stopped at the breakpoints regardless how I tried to place them.

Anyhow. I’ve read quite a few posts even on StackOverflow about the documents() call, but I could not find a clear indication as to why I can’t read the file in the “Documents” folder using the documents() call, and I have to use currentApp() instead.


#7

You could require() json files, providing a path relative to your script. Similarly, to load an image you can use the image-source API to load images relative to your script. If you want to operate with a different type of file, then using currentApp() will yield the files available under my-app/app/


#8

Great idea Pete.K! Thanks!


#9

Hi all,

I know this topic is old but I am still struggling with the same problem. And i have no idea why is not working.
I tried to apply the same logic pentool applied.

I’ve moved the json file to a new document unde app called “documents”

let file = fs.knownFolders.currentApp().getFile("documents/pages.json");

And reading the contents of the file like this:

return new Promise<Object>((resolve, reject) => {
            file.readText().then((content: string) => {
                let data = <Array<Object>>JSON.parse(content);
                resolve(data);
            })
                .catch((err) => {
                    reject(err);
                });
        });

But i always get back undefined

pentool, how did you manage to read the document?


#10

This is how I did it:

yourReadFileFunction() {
	let documents = fs.knownFolders.documents();
	let file = documents.getFile("yourDataFile.txt");
	let jsonData = locations.map(item => item);
	let strData = JSON.stringify(jsonData);

	file.writeText(strData)
        .then(() => {
		console.log("Your data has been saved.");
	})
	.catch((error) => {
        console.log(error);
    });
});

#11

What does this line do? and where does locations object comes from? i was unable to find the module :slight_smile:

So if i use fs.knownFolders.documents(); Then it doesn’t matter in which folder i add my json file?


#12

Sorry, it was late and I just copy/pasted the sample from my project without paying much attention to details. That line you quoted is irrelevant. I simply write json data out to a file which is located in the documents folder. You asked how to access the documents folder, so that was the point of the example. I guess I should’ve just pasted these two lines:

let documents = fs.knownFolders.documents();
let file = documents.getFile("yourDataFile.txt");

And that requires the import of the file system module:

const fs = require("tns-core-modules/file-system");

PS: I haven’t looked at NS for a few months now, and it seems like they’ve updated their documentation. I think it shows how to access the docs folder well:
https://docs.nativescript.org/ns-framework-modules/file-system