Problem with understanding how to read a Json File

file-system

#1

Hi there,

I am facing a very annoying problem for quite some time. I am unable to read a local json file that i have in in my app folder. I even tried to just change it to a plain text file but still the problem is that it always returns undefined.

I tried both from currentApp() and documents(). right now the sotry.txt stays directly under app folder. any Kind of input will be extremely useful. I read the documentation on the File System on Nativescript, but somehow i am still unable to really understand it. I need an example or something on how to use it.

        let fileSystemModule = require("tns-core-modules/file-system");
        let documentsFolder = fileSystemModule.knownFolders.currentApp();
        let path = fileSystemModule.path.join(documentsFolder.path, "story.txt");

        let file = fileSystemModule.File.fromPath(path);

        let jsonFile: string;

        file.readText().then((res) => {
            // Succeeded read from file.
            jsonFile = res;
        });

        console.log(jsonFile);

        let documents = fileSystemModule.knownFolders.documents();
        let file2 = documents.getFile("story.txt");

        file2.readText().then((res) => {
            // Succeeded read from file.
            jsonFile = res;
        });

        console.log(jsonFile);

#2
  1. Which path is returning you undefined? If you have placed the file in app folder, it should work when you use currentApp folder.
  2. Are you using webpack & running release build? If yes, did you make make sure you updated webpack config to pick that file up?
  3. Which platform you are facing this issue with?

#3

I will try to answer as best as i can

Which path is returning you undefined? If you have placed the file in app folder, it should work when you use currentApp folder.
caputre2
When i try to readText on the FilePath it is always undefined ( it doesn’t even enter is the piece of code jsonFile = res; )

        file.readText().then((res) => {
            // Succeeded read from file.
            jsonFile = res;
        });

because after i have console.log(jsonFile); and this always display undefined in the debugger.

Are you using webpack & running release build? If yes, did you make make sure you updated webpack config to pick that file up?

Nope, i just started from a basic empty app template. I tried to keep everything easy.
later edit: yes, i have a file webpack.config.js but i have no idea what i have to do there.

Which platform you are facing this issue with?

Android only

I have some few more questions:

  1. Does currentApp(); points to the app folder in the project? If yes, if i have another folder, lets say “docs” then i will need to concatenate this folder to the whole path, right? /docs/story.txt

  2. Is there another way to load the content of a file in a string variable? (or Json Array :slight_smile: )

Thank you for your help!


#4

currentApp() points to app folder only, we don’t usually write anything to this folder at run time as it’s read only at least on iOS. documents() is usually used to store user related files at run time.

Since you have placed your file inside app folder it should work. Are you using tns run ios | android to run your app - if Yes, it is not web pack specific as you are running simple debug build.

An easy alternative is, create a JS file may be story.js and export your JSON data like,

module.exports = [
{
 ....
}
]

Now you can simply access your JSON by let json = require('~/story')


#5

Omg … yes the alternative is much easier to work with. And now i am just sitting here feeling stupid that i wasted so much time trying to use the file system module. Thank you !!!