I have sent a post request to node express server
app.post('/addUsers', function (req, res) {
var newuser = req.body;
console.log(newuser);
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
data = JSON.parse( data );
data["user4"] = newuser // how can i create a user4 here currently i get an error as newsier is whole req body or something .
console.log( data );
// res.end(data);
res.end( JSON.stringify(data));
});
})
and i have got the value of newuser to console as
{ '{"user4":{"password":"killer","id":4,"profession":"kuchbhi","name":"mukesh"}}': '' }
The question is how can i get the value of user4 so that i can add it to the list of users .
I was using this dictionary in swift
["user4":["name" : "mukesh",
"password" : "killer",
"profession" : "kuchbhi",
"id": 4]]
which i changed to this once knowing its not valid json
["name" : "mukesh",
"password" : "killer",
"profession" : "kuchbhi",
"id": 4]
and know i don't get the error but the data from server response (i have printed it in Xcode console)after adding the fourth user is quite not in the format i think it should be , it looks like this
["user2": {
id = 2;
name = suresh;
password = password2;
profession = librarian;
}, "user4": {
"{\"password\":\"killer\",\"profession\":\"kuchbhi\",\"id\":4,\"name\":\"mukesh\"}" = "";
}, "user1": {
id = 1;
name = mahesh;
password = password1;
profession = teacher;
}, "user3": {
id = 3;
name = ramesh;
password = password3;
profession = clerk;
}]
all other entries are added by me manually in users.son. This is what is logged into terminal console
user4: { '{"password":"killer","profession":"kuchbhi","id":4,"name":"mukesh"}': '' } }
which i still think is not good json or is it ?
Thank you quite a novice with format conversions and servers get/post thingy .