1

Inside my loop I am creating JSON objects like so

JSONObject = {
   "manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
   "model": JSON.parse(object.deviceSpecificationJson[0]).model,
   "capacity": object.capacity[0],
   // etc
}

But node throws an error when capcity is undefined, some of the devices in my XML which I converted to json may not contain a value in the capacity field.

How do I tell node to keep going even if the field is undefined?

4 Answers 4

1

In JavaScript, you can use the ternary operator on object.capacity, like so: object.capacity ? object.capacity[0] : null. This checks whether object.capacity exists, and executes the line after ? if it does, and executes the line after : if it does not.

More generally, this is how you use the ternary operator, {condition} ? {if true} : {otherwise}

An alternative would be to define a function that checks for object.capacity and returns null or whatever default value you want the object property to have if it does not exist.

I have modified your example with the changes below:

JSONObject = {
    "manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
    "model": JSON.parse(object.deviceSpecificationJson[0]).model,
    "capacity": object.capacity ? object.capacity[0] : null,
    // null can be replaced with the value you want it to show if the property does not exist
    // like 0, or an empty string ''
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Easiest way i can think of is to use && and ||

JSONObject = {
   "manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
   "model": JSON.parse(object.deviceSpecificationJson[0]).model,
   "capacity": (object && object.capacity[0]) || 'value in case object.capacity is undefined',
   // etc
}

5 Comments

its a string inside an array such as '32GB', the error I get is TypeError: Cannot read property '0' of undefined and node exits. I don't understand why you put (object && object.capacity[0]) what is the object && part for?
object && checks for the object existence. so if object is a falsy value than the object.capacity[0] will not be executed
If the object was named differently like result.capacity[0] would it then be (result && result.capacity[0])
@Scott yes. it is called short circuiting which is same as logical and gate. where if any of the input is false output turns out to be false. irrespective of other input
I tried "storageSize": (product && product.vertical[0].storageSize[0]) || "" and got the same error Cannot read property '0' of undefined ?
0

You'll need to put one condition to check undefined

JSONObject = {
  "manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
  "model": JSON.parse(object.deviceSpecificationJson[0]).model,
  "capacity": object.capacity[0]!= undefined ? object.capacity[0]: 'your static value for undefined capacity like null' ,
  // etc
}

Comments

0

You can conditionally add capacity to your JSONObject like so:

JSONObject = {
   "manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
   "model": JSON.parse(object.deviceSpecificationJson[0]).model,
   // etc
}
if(object.capacity[0]) JSONObject["capacity"] = object.capacity[0]

Or, if you just want to set the capacity to be 0GB if it doesn't exist you can do the following:

JSONObject = {
   "manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
   "model": JSON.parse(object.deviceSpecificationJson[0]).model,
   "capacity": object.capacity[0] || '0GB',
   // etc
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.