Reputation: 707
I have a form on submit of the form I am receiving the json object after form submit. What I am trying is that I have a file data.json in my application folder in "/src/app/data.json". How can I write the data into the JSON file with Node.
Below is my code
Below is the code in my component.ts
savedatatojson(jsonobject){
// jsonobject is the json data
this.dataservice.savedata(jsonobject).subscribe(data => {
console.log(this.data)
})
}
Below is the code in my service file
public savedata(jsonobject):Observable<any>{
//code to be written
}
How do I save data to json file which I have obtained in my service file
Upvotes: 3
Views: 7426
Reputation: 2105
You can't, web browsers don't have permissions to write to source files. There does exist a file system api, however this is non-standard and can only access a very limited location of files.
Instead you should either send the data to a database which later you can read and write to a file, or generate a file within the application which users could download for themselves to their own computer downloads folder. There is also the option to use serverless functions to update a remote file on a server, or for example a google sheet.
Upvotes: 4