'Modifying values in json responses
I'm looking to modify a few values in a returning json api response that gets intercepted from the endpoint and passed through a server back to my app.
I can get it to work with changing the user part of the json response but I can't get it to include the resident part of the response
The following code changes user.require_pin and user.enable_logs from true to false. I'm looking to add resident.login_active to false from true in the same response but each time I try, I receive the error : resident: { ^^^^^^^^
SyntaxError: Unexpected identifier How do I add it? Here is my current code and same json response. Thanks in advance!
let dat = response.data
if(
(req.path === '/sessions' || req.path === '/sessions/')
&& (req.method === 'POST')
) {
dat = {
...dat,
user: {
...dat.user,
require_second_pin: false,
enable_photo_log: false
}
resident: {
...dat.resident,
login_active: false
}
}
}
This is a sample of the server json response
{
"token": "19f2ef14-14d7-4653-907e-1b5243900e96",
"id": "19f2ef14-14d7-4653-907e-1b5243900e96",
"warning": "",
"resident": {
"id": 18016522,
"resident_id": 264668672,
"name": "JOHN DOE",
"login_active": true
},
"user": {
"id": 12727533,
"transfer_photo": false,
"enable_log": true,
"photo_capture_interval": 10,
"no_of_photos": 1,
"require_pin": true
}
}
Solution 1:[1]
You are just missing a comma:
let dat = response.data
if(
(req.path === '/sessions' || req.path === '/sessions/')
&& (req.method === 'POST')
) {
dat = {
...dat,
user: {
...dat.user,
require_second_pin: false,
enable_photo_log: false
}, // <-- this comma was missing
resident: {
...dat.resident,
login_active: false
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Christian Fritz |
