Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Vue handle null in nasted array

I'm from Ruby language so sorry for noob question. I've got this response from API:

[{:id=>"61b79d02a0f6af001374744e",
  :name=>"Waffle Crewneck",
  :code=>"FW22KS000",
  :result=>{"status"=>"Success", "message"=>"FW22KS000 - Synced"},
  :last_sync_at=>Wed, 18 May 2022 23:51:45.195079000 UTC +00:00},{:id=>"611ea9a7392ab50013cf4713", :name=>"2-Tone Hoodie", :code=>"SS22CH013", :result=>nil, :last_sync_at=>nil},]

Which I want to display inside of the table:

<tr v-for="product in fetchedProductSyncStatus" :key="product">

<td class="text-bold">
  {{ product.result.status }}
</td>
<td class="text-bold">
  {{ product.result.message }}
</td>
<td class="text-bold">
  {{ product.last_sync_at }}
</tr>

Because in second array element product.result == nil I've got an error:

TypeError: Cannot read properties of null (reading 'status')

Looking for something similar to Ruby's product.result&.message. How to avoid such error in Vue and display empty string instead?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There's the same thing in TypeScript, called optional chaining. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

{{ product.result?.message }}

Or the old-style product.result && product.result.message

Edit: Optional chaining in templates only works in Vue3

about 4 years ago · Juan Pablo Isaza Report

0

I believe this is more of a javascript rather than Vue issue... you can do something like this to check if product.result is available:

{{product.result ? product.result.status : null}} or

{{ product.result && product.result.status }}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!