amu03
amu03

Reputation: 371

Getting this error: Cannot read property 'length' of undefined

I am building an app with nuxt (using vuetify 2 and typescript).

I have radio buttons(say b1 b2) and text fields(say t1 t2 t3).

When user clicks b1, it shows t1 and t3

When user clicks b2, it shows t2 and t3

I think validation for field t1 is causing an error but not sure why.

When user open the page, b1 is selected by default.

When I switch to b2, and write something... then comeback to b1, nothing happen. It works as desired.

When I switch to b2 then switch back to b1 without writing anything, I get Cannot read property 'length' of undefined error.

I first thought that I got this error because id is undefined but the default value of id is 'test' and the testdata is already initialized at the time I switch back to b1 so I am confused...

Here's my simplified code:

<template>
  <v-app>
    <v-container justify="center">
      <v-card flat width="400" class="mx-auto mt-5 mb-6">
        <v-card-text>
          <v-form v-model="isFormValid" class="pa-3">
            <v-radio-group v-model="testdata.type" row>
              <v-radio label="b1" value="b1"></v-radio>
              <v-radio label="b2" value="b2"></v-radio>
            </v-radio-group>
            <v-text-field v-if="testdata.type == 'b1'" v-model="testdata.id" counter :rules="[rules.required, rules.id]" />
            <v-text-field v-if="testdata.type == 'b2'" v-model="testdata.id2" :rules="[rules.required]" />
            <v-text-field v-model.number="testdata.price" type="number" :rules="[rules.required]"/>
          </v-form>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="primary" @click="somemethod()" :disabled="isLoading || !isFormValid" :loading="isLoading">submit</v-btn>
          <v-spacer></v-spacer>
        </v-card-actions>
      </v-card>
    </v-container>
  </v-app>
</template>
<script lang="ts">
import Vue from 'vue';

interface SomeDto {
 type: 'b1'|'b2'; id:string; id2:string; price:number
}

interface Data {
  rules: any;
  isFormValid: boolean;
  testdata: SomeDto;
  isLoading: boolean;
}
export default Vue.extend({
  data(): Data {
    return {
      isFormValid: false,
      isLoading: false,
      testdata: {type: 'b1', 'id:'test', id2:'', price:0},
      rules: {
        required: (value: any) => !!value || 'required',
        id: (value: string) => value.length == 12 || 'you need 12 characters',
      },
    };
  },
  methods: {
    async somemethod() {
    //do something
    },
  },
});
</script>

Any help would be appreciated!!!

Upvotes: 2

Views: 1664

Answers (2)

saddam
saddam

Reputation: 196

from :-

id: (value: string) => value.length == 12 || 'you need 12 characters'

to :-

id: (value: string) => (value && value.length == 12) || 'you need 12 characters'

Here, first it will check value after it will check length if both are true then it will become true.

Upvotes: 1

Mobin Samani
Mobin Samani

Reputation: 275

Just change

id: (value: string) => value.length == 12 || 'you need 12 characters'

to

id: (value: string) => value && value.length == 12 || 'you need 12 characters'

Upvotes: 3

Related Questions