Context : Angular 5 app, NodeJS (with Nest) backend, MongoDB (Mongoose)
- When I
console.logthe server's response, it looks fine When I assign this very same response to a variable then
console.logthe variable, I get"lang":undefinedinstead of"lang":"cn". This blows my mind.this.http .post(route, payload) .subscribe(res => { // logs '...."lang":"cn"....' (correct) console.log(`res._body = `, res["_body"]) // logs { ... "lang":"cn" ... } (correct) console.log(`JSON.parse = `, JSON.parse(res["_body"])) // logs { ... "lang":"cn" ... } (correct) console.log(`res.json = `, res.json()) let response = res.json() // Logs { ... "lang": undefined ... } (WTF?) console.log(`response = `, response) })
Why is lang undefined after assigning res.json() to a variable?
It does the exact same thing with JSON.parse().
Here's more log : ( I have removed what I believe to be irrelevant parts of the JSON data)
RES._body = {"translations":[{"score":3,"date":"Tue Apr 10 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)","expr":"'風力発電' OR '風力発電機'","lang":"cn","langFull":"Simplified Chinese (中文)","dateHuman":"a month ago"},{"score":5,"date":"Tue Apr 30 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)","expr":"pale OR lame OR hélice","lang":"fr","langFull":"French (Français)","dateHuman":"8 days ago"},{"score":10,"date":"Tue Apr 19 2018 11:35:36 GMT+0200 (W. Europe Daylight Time)","expr":"blade OR strip OR (winding* NEAR2 (turbine OR power OR generator))","lang":"en","langFull":"English","dateHuman":"19 days ago"}]
JSON.parse = { "translations" : [
{
"score": 3,
"date": "Tue Apr 10 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "'風力発電' OR '風力発電機'",
"lang": "cn", // <--------------------------- HERE OK
"langFull": "Simplified Chinese (中文)",
"dateHuman": "a month ago"
},
{
"score": 5,
"date": "Tue Apr 30 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "pale OR lame OR hélice",
"lang": "fr",
"langFull": "French (Français)",
"dateHuman": "8 days ago"
},
{
"score": 10,
"date": "Tue Apr 19 2018 11:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "blade OR strip OR (winding* NEAR2 (turbine OR power OR generator))",
"lang": "en",
"langFull": "English",
"dateHuman": "19 days ago"
}
]}
res.json() = { "translations": [
{
"score": 3,
"date": "Tue Apr 10 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "'風力発電' OR '風力発電機'",
"lang": "cn", // <--------------------------- HERE OK
"langFull": "Simplified Chinese (中文)",
"dateHuman": "a month ago"
},
{
"score": 5,
"date": "Tue Apr 30 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "pale OR lame OR hélice",
"lang": "fr",
"langFull": "French (Français)",
"dateHuman": "8 days ago"
},
{
"score": 10,
"date": "Tue Apr 19 2018 11:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "blade OR strip OR (winding* NEAR2 (turbine OR power OR generator))",
"lang": "en",
"langFull": "English",
"dateHuman": "19 days ago"
}
]}
response = { "translations": [
{
"score": 3,
"date": "Tue Apr 10 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "'風力発電' OR '風力発電機'",
"lang": undefined, // <--------------------------- HERE WTF???
"langFull": "Simplified Chinese (中文)",
"dateHuman": "a month ago"
},
{
"score": 5,
"date": "Tue Apr 30 2018 13:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "pale OR lame OR hélice",
"lang": "fr",
"langFull": "French (Français)",
"dateHuman": "8 days ago"
},
{
"score": 10,
"date": "Tue Apr 19 2018 11:35:36 GMT+0200 (W. Europe Daylight Time)",
"expr": "blade OR strip OR (winding* NEAR2 (turbine OR power OR generator))",
"lang": "en",
"langFull": "English",
"dateHuman": "19 days ago"
}
]}