Encountering the "Vue is not a function" issue while using my to-do list application

I've created a to-do app where clicking on an item should mark it as completed.

However, I encounter an error when trying to toggle the completion status of an item imported from Firebase:

Error in v-on handler: "TypeError: todo.toggleChecked is not a function"

As a newcomer to Firebase and Vue, I'm unsure why this issue is occurring.

Below is the code snippet in question:

TodoList.vue

<template>
  <div class="todolist">
    <h3>TodoList</h3>
    <b-input-group class="mt-3">
      <b-form-input
        v-model="todoInput"
        placeholder="Add task"
        aria-describedby="Add task"
      ></b-form-input>
      <b-input-group-append>
        <b-button variant="primary" @click="addTodo()">Add</b-button>
      </b-input-group-append>
    </b-input-group>
    <b-list-group>
      <b-list-group-item
        v-for="(todo, index) in todoList"
        :key="index"
        class="d-flex justify-content-between align-items-center mt-3"
        :variant="listItemStyle(todo.checked)"
        @click="toggleCompletion(todo)"
      >
        {{ todo.todo }}
        <b-button variant="outline-dark" @click.stop="deleteTask(todo)" pill
          ><font-awesome-icon icon="fa-solid fa-trash"
        /></b-button>
      </b-list-group-item>
    </b-list-group>
  </div>
</template>

... (rest of the component code)

In addition, I'm struggling to find documentation for this particular syntax style. Most tutorials use a different approach like:

app
   .firebase()
   .collection('collectionName')
   .add(itemToAdd)
   .then((result) => { moreCode })

Unfortunately, this standard format doesn't seem to work with my project.

UPDATE: I managed to resolve the issue by modifying the way I retrieve values from Firestore using the following syntax:

async function getTodo(db: Firestore) {
      const todoCol = collection(db, "todolist");
      const todoSnapshot = await getDocs(todoCol);
      const todoList = todoSnapshot.docs.map((doc) => {
        let docTodo = new Todo(doc.data()['todo']);
        docTodo.checked = doc.data()['checked'];
        return docTodo;
      });
      return todoList as Todo[];
    }

Answer №1

It seems like the items retrieved from the database in the mounted function may not be instances of the Todo class. Even though you are casting them as Todo[], they are likely just raw data without the toggleChecked method defined on them.

To create actual Todo type objects from the fetched data, you can do something similar to this:

async function getTodo(db: Firestore) {
    // ...
    const todoList = todoSnapshot.docs.map(doc => {
        // extract 'todo' and 'checked' properties from doc and use them to create a new Todo object
        return new Todo(doc.todo, doc.checked); 
    }

    return todoList as Todo[];
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am experiencing an issue in Angular 6 where the close button is unresponsive when I add a carousel within a

I successfully added a carousel in a modal popup and it is working properly. However, I encountered an issue when trying to close the modal using the close button - it is not functioning. Here is a link to the stackblitz demo for reference. You can also ...

having difficulty interpreting the information from angular's httpclient json request

After creating an Angular function in typescript to make an http request for JSON data and save it to an object, I noticed that the function requires two clicks of the associated button to work properly. Although the connection and data parsing are success ...

"I encountered an error while sorting lists in Vue 3 - the function this.lists.sort is not

Creating a Vue 3 front-end template: <template> <div class="container"> <router-link to="/user/create" class="btn btn-success mt-5 mb-5">Add New</router-link> <table class=" ...

Checking for the existence of a Vue.js component

In the ready: method of the root instance, I have certain tasks to perform but only if some components are not present (not declared in the HTML). Is there a way to verify the existence of a component? ...

Capture and handle JavaScript errors within iframes specified with the srcDoc attribute

My current project involves creating a React component that can render any HTML/JavaScript content within an iframe using the srcDoc attribute. The challenge I am facing is implementing an error handling system to display a message instead of the iframe ...

Steps to deactivate a button upon clicking in vue.js

I need assistance with my voting application. I am looking to implement a feature that disables the voting button after it has been clicked. Can someone provide guidance on how to achieve this? template <v-btn v-for="choice in data.choices" @c ...

Utilizing Vue.js: Dynamically linking v-model to route parameters depending on the current state

I'm currently in the process of developing an application that will serve as the backbone for a restaurant chain's website. The main functionality involves allowing users to modify page content and images. Given the complexity of the site with it ...

Applying the spread operator in the map function for copying objects

In my Angular application, I am attempting to copy an object and add a new property using the spread operator. To add the new property, I have created a method called 'addNewProperty(name)' which returns the property and its value. However, when ...

Utilizing declaration files in a TypeScript application to provide global exposure of types

Recently, I've delved into the world of typescript and set up a project with numerous React components written in typescript. To streamline development, we decided to extract all necessary types for these components into a separate file named types.d. ...

Adding an arrow to a Material UI popover similar to a Tooltip

Can an Arrow be added to the Popover similar to the one in the ToolTip? https://i.stack.imgur.com/syWfg.png https://i.stack.imgur.com/4vBpC.png Is it possible to include an Arrow in the design of the Popover? ...

Is it possible to utilize a library function without needing to invoke the 'this' keyword?

Is it possible to call .size(x) without calling .size() if lodash is imported in 'main.js' and used in my component? I've received advice to import lodash in my component, but I prefer a method that works across all components. Appreciate a ...

What is the best location to initialize Firebase in a React Native application?

What is the best way to initialize Firebase in a React Native project and how can I ensure that it is accessible throughout the entire project? Below is my project structure for reference: Project Structure Here is a basic template for initializing Fireb ...

What could be causing the error that appears when I execute the "php artisan" command?

Encountering an issue with artisan/composer; attempting to execute commands such as "php artisan" or run a migration like "php artisan make: migration" results in an error message: SQLSTATE [42S02]: Base table or view not found: 1146 Table https://i.ssta ...

Using the Postman application, the Sequelize delete request with payload functions correctly; however, it encounters issues when executed through the Vue

Struggling to send PUT and DELETE requests via my express backend to a sqlite database. PUT request is working, but DELETE request consistently fails. Verified headers in the network tab, both are correct (application/json). Postman can successfully delet ...

Issue with handling data from web api in Angular 6

I've been working on processing data from an API using Angular 6. Despite seeing that the data is being returned in the Network tab, I'm having trouble processing it after the call is complete. The data returned by my service: {"profile": " ...

Utilizing the class decorator pattern in TypeScript with a recursive original class method: A guide

For clarity, I am utilizing the decorator pattern/approach from and not the experimental decorators feature in TypeScript. The scenario involves extending the next method on the main class Foo. Various features are implemented by different classes like B ...

Preventing unauthorized users from accessing routes and child components in Angular 2

Here is the project structure: AppComponent Nav Component LoginComponent Once the user successfully authenticates through the form connected to Firebase, they are directed to the section accessible only by logged-in users. AccountComponent Profile ...

Determining the length of an array of objects nested within another object

I received a response from the API called res. The response is in the following format: {"plan":[{"name":"ABC"},{"name":"DEF"}]}. I am attempting to save this response in my TypeScript code as shown below: ...

A helpful guide to adjusting the cursor placement within a contenteditable div using React

I am developing a custom text editor using a contenteditable div. Each time a user modifies the text inside it, I aim to enclose all the new text with a strong element and update the div's innerHTML accordingly. Here is what I have attempted (utilizi ...

What is the best way to combine vuetify.css and Bootstrap on a single webpage?

Is it possible to simultaneously incorporate the vuetify.min.css and bootstrap.min.css folders in a project? Bootstrap is currently implemented on the layout page while vuetify.min.css is needed for another specific page. How can both of these CSS framew ...