Search code examples
ionic-frameworkionic-native

Ionic Native File append: true throwing error "Not Found" if the file is not there


I am trying File.writefile from Ionic Native and I'm having issues with the weather parameter.

Here's the link to the docs - https://ionicframework.com/docs/v3/native/file/

File.writeFile('file:///storage/emulated/0/Documents/', 'result.txt', `testing123\n`, {append: true}).then(succ=>{
      alert("File write success : " + JSON.stringify(succ))
    },
    err=>{
      alert(" write File error : " + JSON.stringify(err))
});

When I use {replace: true} it will write a new file every time but not append the data.

If I use {append: true} then it won't create a file on the file run...it has to exist.

How do I get it to create a file if it doesn't exists and append to it?


Solution

  • As a work around, trap the error, create the file and recall your function.

    Also use await as it increases readability.

    await File.writeFile('file:///storage/emulated/0/Documents/', 'result.txt', `testing123\n`, {append: true}).catch (e) {
         await File.writeFile('file:///storage/emulated/0/Documents/', 'result.txt', `testing123\n`, {append: false});
    }