keep response key nodejs sequelize on null with vue2

Viewed 19

my response json

if null

dm_personal: null

if not null if null

dm_personal: { id: 1 }

how to keep the key of response on null ?

or how to implement on vue2 if i fetch / load single data that set data() { return {} }

when its null it shown cannot read on null

here is my input

  <vue-autosuggest
   id="vi-dm_personal-penanggung_jawab"
   v-model="form.transportir.dm_personal.full_name" // here is problem null
   :suggestions=" [{ data: form.transportir.dm_personal.dm_personals }]" // here is problem null
...

in method or other we can use elvis operator like data?.data but in html element for v-model or any properties ( like :suggestions ) , how to best practice to clear the problem ???

1 Answers

Vue will only create the property if the parent object already exists but it will not create if the parent property value is null.

form: {
  transportir: {
    dm_personal: {}
  }
}

So what you can do it convert the null into an object literal.

Live Demo :

new Vue({
  el: '#app',
  data: {
    form: {
        transportir: {
        dm_personal: null
      }
    }
  },
  created() {
    if (!this.form.transportir.dm_personal) {
        this.form.transportir.dm_personal = {}
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="text" v-model="form.transportir.dm_personal.full_name"/>
</div>

Related