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

javascript - three.js lot of meshes optimization -

smartface.io - Proper way to change color scheme for whole application -

Email notification in google apps script -