0

So, I have my JSON file:

{
   HomeWork: []
}

And I want to push an object: {Title: T, Due: D, Description: Desc} To the JSON file, How should I do that? I am using this right now:

async function AddWork(obj) {
workData.HomeWork.push(obj);
//RNFS.writeFile('../../json/work.js', JSON.stringify(workData));
}

I tried whit the RNFS line, but it did not help.

Yared
  • 2,206
  • 1
  • 21
  • 30

1 Answers1

0

If you want to do that, there is no edit mode ! so be careful .

If you read it and modify it , you are doing it locally, so you need override the file you are working on.

you need to read it using Read File in fs and then after you do the modification you need to Write it on the top of the file you are working with

I hope it helps <3

Amir Doreh
  • 1,369
  • 1
  • 13
  • 25
  • Thanks but how should i do it?, I am new to working with files. – Blue Syntax Jul 13 '20 at 08:03
  • as you mention rnfs i gave you the docs for that, i personally recommend using RNFetchBlob instead of RNFS! read the documentation its clear , if you got any problem write here i will help you. this link can help you https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#writefilepathstring-contentstring--array-encodingstring-appendbooleanpromise – Amir Doreh Jul 13 '20 at 08:23
  • I am really sorry, i tried to read the docs but i suck at it. I really don't have any idea on how to do it. :( – Blue Syntax Jul 13 '20 at 08:37