I am new to angularJs and i am using Ionic framework.
i have an existing JSON file. that is like this
[{"name":"sample"}]
When i append the file with new data, this is how it get appended
[{"name":"sample"},[{"name":"sample2"}]]
but i want my file to be like this
[{"name":"sample"},{"name":"sample2"}]
here is my code:
var savearray =[];
$cordovaFile.readAsText('user.json')
.then(function (success) {
$scope.quotes = JSON.parse(success);
savearray.unshift(JSON.parse(success,true));
console.log("Successful in reading the file initially:"+JSON.stringify(savearray));
}, function (error) {
console.log("error reading the file"+JSON.stringify(error))
});
and this is how i write my data to the file.
$scope.saveQuote = function(data){
savearray.unshift(data);
$cordovaFile.writeFile('user.json',JSON.stringify(savearray),{append:false})
.then(function (success) {
console.log("success the file has been created")
}, function (error) {
console.log("error"+JSON.stringify(error))
});
}
I would be glad if anyone could help me. thanks.