Storage write issue

I am using Fuse to develop a new version of my app ‘contractnotify’. The UI is ready and i am now hooking up our API. Everything is working so far. I login making a POST request. I get a token back. This all works fine. Problem is when i try to write the token (or anything else) to a file called filedb.txt nothing happends. I am working on a mac so i first thought it had to do with file permissions, but i put everything on read/write and still no luck. Maybe my code is wrong, please advise. Thank you!

My code:

var Observable = require("FuseJS/Observable");var requestObject = {'username':'myusername', 'password':'mypassword'};

fetch('https://www.contractnotify.com/api/login', {
    method: 'POST',
    headers: { "Content-type": "application/json"},
    dataType: 'json',
    body: JSON.stringify(requestObject)
}).then(function(response) {
    status = response.status;  // Get the HTTP status code
    response_ok = response.ok; // Is response.status in the 200-range?
    return response.json();    // This returns a promise
}).then(function(responseObject) {
    // Do something with the result
    console.log("Success"); //this works i get a success
    var thetoken = responseObject.token; // this is the token i get
    console.log(thetoken); //this shows the token and works
    var storage = require('FuseJS/Storage'); //require storage
    storage.write("/filedb.txt", thetoken); //write to file

}).catch(function(err) {
    // An error occured parsing Json
    console.log("error", status);
});

Not 100% sure what the problem is, but:

storage.write("filedb.txt", thetoken).then(function (success)
{
    if (success) {
        console.log ('Stored file');
    } else {
        console.log ('Storing file FAILED');
    }
})

This will give you an indication of whether the operation failed or succeeded.

I would try removing the leading slash / from the filename, I think that could easily be the problem. Fuse currently stores files at the strangest locations on Windows by default, and it wouldn’t surprise me if trying to force an absolute path on Mac would cause it to fail.

Thank you for your reply. You understood correctly. Strange enough LOG result is ‘Stored file’, but there is nothing written to the file. I searched for filedb.txt on my mac (to check it was maybe stored somewhere else) but there is only the one i added and it’s empty. Any additional ideas? Thanks again!

what do you get in your log?

When i add this code i get ‘Stored file’. So fuse things it’s written, but file is empty.

Any update to this? I have the same problem.

I’m currently making a ticket for this internally so we can get more eyes on it.

Thanks @Jake, looking forward for an update.

@wouter I suspect two issues in your code.

  1. Leading slash. This will try to create /filedb.txt, and hopefully you don’t have access to that with the user you are running fuse under.

  2. Unknown type of thetoken, the Storage API only works with strings.

Solution:

storage.write("filedb.txt", "" + thetoken); //write to file

@ck3k:

Can you post a complete test-case that demonstrates the problem? This code works for me:

<App Theme="Basic">
    <JavaScript>
      var requestObject = {'username':'myusername', 'password':'mypassword'};
      var storage = require('FuseJS/Storage'); //require storage
      storage.write("filedb.txt", JSON.stringify(requestObject)); //write to file
     </JavaScript>
</App>

And the file is in ~/.local/share/filedb.txt

$ cat ~/.local/share/filedb.txt
{"username":"myusername","password":"mypassword"}

Hey @Bjorn. Thank you for your reply. It was up to me I guess. I thought the file will be saved in project directory and I haven’t seen any details of the saved file location on API page. I’ve just cheked and the file is there and it’s written properly.

One note, the same directory for Windows is located @ %AppData%\Local.

Thanks for the help, appreciated!

Hey @Bjorn, thank you for your time. I will look into it tonight!

This worked for me: storage.write("filedb.txt", "" + thetoken);

i thought the the filedb.txt would be in the folder. But it was stored like Bjorn mentioned, which was a bit difficult to find.

When i now read filedb.txt with the following code, i will get my saved token:

Storage.write("filedb.txt", "" + thetoken).then(function(wasSuccess) { if(wasSuccess) { Storage.read("filedb.txt").then(function(content) { storedToken = content; console.log('storing token success, token = ' + storedToken); }); } });

Thank you!