Hello so I am not understanding how to fetch a JSON file locally and display the data; I understand I need to use
var Storage = require("FuseJS/Storage");
I’m just trying to do what the different colors JSON through HTTP are doing, except I am not understanding how to do it locally. Currently the json file is colors.json and the app goes as followed:
If someone could point in the right direction of how to observe the json file locally that would be fantastic, the documentation didn’t really help me as there isn’t much explanation for it.
And when I try writing to a text file the code isn’t working even though I have the file mytxtfile.txt listed as for where it shoud go (this part is litterally straight from the documentation) it says in the console that it wrote it successfully, but when i got to the file nothing is there.
<App>
<JavaScript>
var Storage = require("FuseJS/Storage");
var success = Storage.write("mytxtfile.txt", "Hello from Fuse!");
if(success) {
console.log("Successfully wrote to file");
}
else {
console.log("An error occured!");
}
</JavaScript>
</App>
The docs explain it correctly, you used them wrong:
(One thing they don’t explain, but not sure if its true: I think reading for storage will only work for files you written via the Storage api, try using the bundle API)
var Bundle = require('FuseJS/Bundle');
Bundle.read('color.json')
.then(function(contents) {
// you should log what contents is (which should be a string, which means it doesn't have a json method)
data.value = JSON.parse(contents);
});
and for writing:
var Storage = require('FuseJS/Storage');
Storage.write('mytxtfile.txt', 'Hello, from Fuse!')
.then(function(success) {
success // boolean
// do your check inside here
});
For Fuse Team Reading This: The write api should change, instead of returning a success boolean, the promise should resolve if successful returning the content back, and reject if failed returning the error.
The docs explain it correctly, you used them wrong:
(One thing they don’t explain, but not sure if its true: I think reading for storage will only work for files you written via the Storage api, try using the bundle API)
var Bundle = require('FuseJS/Bundle');
Bundle.read('color.json')
.then(function(contents) {
// you should log what contents is (which should be a string, which means it doesn't have a json method)
data.value = JSON.parse(contents);
});
and for writing:
var Storage = require('FuseJS/Storage');
Storage.write('mytxtfile.txt', 'Hello, from Fuse!')
.then(function(success) {
success // boolean
// do your check inside here
});
For Fuse Team Reading This: The write api should change, instead of returning a success boolean, the promise should resolve if successful returning the content back, and reject if failed returning the error.
I’m sorry, but it seems like it didn’t want to work still; the mainview is looking like this now, and still doesn’t want to work.
Now I feel like I’m overcomplicating things now, this is not straight forward, and I’m hope that I am overcomplicating things, and this should be easier, hopefully the fuse team can help here.
First off, thanks to Edwin for the correct answer.
This is a common confusion regarding the value property, as it actually refers to the first item of an Observable list. This makes sense for single item observables, but not so much when dealing with several items.
We’re having internal discussions on how to make this more clear.
asaxionis, Tom, Ish, does this resolve your problems?
Regarding the write method of the Storage module, we have soon-to-be-released FileSystem module that aims to replace the Storage module. The write methods of that new API rejects the promise instead of returning a boolean, as you suggested.