P
P
Peter Sergeev2021-12-05 19:09:01
JavaScript
Peter Sergeev, 2021-12-05 19:09:01

Composition API - how to access an object inside it in VUE.js?

Cannot figure out how to resolve this error:
Cannot read properties of undefined (reading 'deadLine')
An error occurred while reading the deadLine property in the deadLineTimestamp property of one data object .
How can this error be resolved?

The task is generally to take from the date and send it to the server in the timestamp format.

<template>
  <form class="card" @submit.prevent="createTask">
    <h1>{{ titleblock }}</h1>
    <div class="form-control">
      <label for="title">Название</label>
      <input type="text" id="title" v-model="title">
    </div>

    <div class="form-control">
      <label for="date">Дата дэдлайна</label>
      <input type="date" id="date" v-model="deadLine">
    </div>

    <div class="form-control">
      <label for="description">Описание</label>
      <textarea id="description" v-model="description"></textarea>
    </div>

    <button class="btn primary">Создать</button>
  </form>
</template>

<script>
import { ref, reactive, toRefs } from 'vue';
import { useStore } from 'vuex';

export default {
  setup() {
    const store = useStore();
    const title = ref('Создать новую задачу!');

    const data = reactive({
      title: '',
      deadLine: new Date(),
      deadLineTimestamp: new Date(this.deadLine).getTime(), //ОШИБКА
      description: ''
    });

    function createTask() {
      store.dispatch('getTasks', data);
    }

    return {
      titleblock: title,
      ...toRefs(data),
      createTask
    };
  }
};
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-12-05
@223606322

Remove deadLineTimestamp from data, pass data to action instead

{
  title: data.title,
  deadLineTimestamp: new Date(data.deadLine).getTime(),
  description: data.description,
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question