简体   繁体   中英

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?

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});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM