javascript - Error on second level object for nodejs response -


i have situation.., i'm making this:

app.post('someurl', function (req, res) {      var r = res.data;      var = {};     a.name = r.name || "",     a.someotherkey : {         id: r.otherkey.id || ""     }  }); 

the thing when res.data == "", can assign value of a.name because r.name "undefined", r.otherkey.id horrible

"typeerror: cannot read property 'id' of undefined" 

any ideas solve problem ???

you take advantage of && operator do:

a.someotherkey = {     id: r.otherkey && r.otherkey.id || "" } 

&& return second value if both true, , false if 1 (or both) false.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -