In NativeScript, the http
module doesn't currently support making network requests to the local file system. That work is intended to be done by the file reader. It's pretty simple to read a local file and parse its contents as JSON.
This is the TypeScript/Promise version of what Emil Oberg created for the same question on StackOverflow. This module should be reusable for any and all asyncronous local JSON read operations.
import * as fs from 'file-system';
var documents = fs.knownFolders.currentApp();
class FileReader {
static readJSON(path: string) {
var jsonFile = documents.getFile(path);
return new Promise<Object>((resolve, reject) => {
try {
jsonFile.readText().then((content: string) => {
let data = <Array<Object>>JSON.parse(content);
resolve(data);
});
}
catch (err) {
reject(err);
}
});
}
}
export default FileReader;
Hi, how can I use it correctly in my views ? Because, an error occured when I'm loaded my view.
My view:
JS ERROR TypeError: FileReader.readJSON is not a function. (In 'FileReader.readJSON("/res/test/test.json")', 'FileReader.readJSON' is undefined)
Thx you for this FileReader :)