1

I'm trying to list the drivers in b-table.

This gives me the correct name of the driver: console.log(this.orders[0].order_drivers[0].populated_driver.user.username)

But when I want to display it on the table, it gives "data.item.order_drivers[0] is not defined" error.

Here's my code:

<b-table :items="orders"
             :fields="fields"
             :per-page="perPage"
             :current-page="currentPage"
             :filter="filter"
             :filter-function="filterDate"
             :sort-by.sync="sortBy"
             :sort-desc.sync="sortDesc"
             sort-icon-left
             small
             >
      <template #cell(status)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.status }}</span>
        </div>
      </template>
      <template #cell(order_number)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.order_number}}</span>
        </div>
      </template>
      <template #cell(deliver_date)="data">
        <span class="align-text-top text-capitalize">{{ data.item.deliver_date }}</span>
      </template>
      <template #cell(pickup_date)="data">
        <span class="align-text-top text-capitalize">{{ data.item.pickup_date }}</span>
      </template>
      <template #cell(subtotal)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.subtotal }}</span>
        </div>
      </template>
      <template #cell(tip)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.tip_from_restaurant }}</span>
        </div>
      </template>
      <template #cell(additional_tip)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.extra_tip_from_restaurant }}</span>
        </div>
      </template>
      <template #cell(tfr_tip)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.tip_from_tfr }}</span>
        </div>
      </template>
      <template #cell(tfr_additional_tip)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.extra_tip_from_tfr }}</span>
        </div>
      </template>
      <template #cell(driver)="data">
        <div class="text-nowrap">
          <span class="align-text-top text-capitalize">{{ data.item.order_drivers[0].populated_driver.user.username}}</span>
        </div>
      </template>
</b-table>

And this is what axios returns: Response

UPDATE:

I've added v-if and v-else as suggested and worked for me!

<div class="text-nowrap">
<span class="align-text-top text-capitalize" v-if="orders[data.index].order_drivers.length!==0" >{{ data.item.order_drivers[0].populated_driver.user.username}}</span>
          <span  class="align-text-top text-capitalize" v-else>None.</span>
</div>
4
  • As per the axios API response. It should work. I am assuming data.item referes to the array objects. Commented Jun 13, 2022 at 10:39
  • 1
    Because data is not there at some point, or it doesn't exist at all for some items. Don't render it if it's not ready to use. That this.orders[0].order_drivers[0] is available means that only this item is available Commented Jun 13, 2022 at 11:00
  • @EstusFlask I got your point. Do you have any suggestions for rendering? Because I need to display the drivers (if exist), or display something like 'NOT ASSIGNED' if there is no drivers assigned yet. Thanks for your help. Commented Jun 13, 2022 at 21:01
  • Then use v-if and v-else. Commented Jun 13, 2022 at 21:02

0

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.