i want write json object file (not .json file) after calling function. when calling function json object, want append object file.
currently using jsonfile that.
var jsonfile = require('jsonfile'); ... users_file = "./users_file"; function update(user_json) { jsonfile.writefile(users_file,user_json), function(err) { console.error(err); }); } but calling update again json object, first line overwritten.
example:
json1 = {"id":123456,"first_name":"john","last_name":"smith","username":"johnsmith"} json2 = {"id":654321,"first_name":"marc","last_name":"cold","username":"marccold"} when calling update(json1) , later update(json2) want file this:
{"id":123456,"first_name":"john","last_name":"smith","username":"johnsmith"} {"id":654321,"first_name":"marc","last_name":"cold","username":"marccold"} currently second line replacing first line. tried read file first , concat both json objects failed. needs work when file empty.
use appendfile() instead of writefile(). writefile() write new file or overwrite existing file if any. while appendfile() append content existing file if or create new file , add content.
Comments
Post a Comment