Issue encountered during rendering: The function used is not a filter

Everything works fine with the search filter and pagination on the initial load.

However, upon clicking to go to the next page, an error occurs stating

**'Error in render: "TypeError: this.tickets.filter is not a function"'**
.

This issue can be found in the Tickets.vue file:

<template>
<div class="container--ticket">
    <nav>
    <ul class="pagination justify-content-end">
        <li v-bind:class="[{disabled: !pagination.prev_page_url}]" class="page-item">
        <a
            class="page-link"
            href="#"
            @click.prevent="fetchTickets(pagination.prev_page_url)"
        >Previous</a>
        </li>
        <li class="page-item disabled">
        <a
            class="page-link text-dark"
            href="#"
        >Page {{ pagination.current_page }} of {{ pagination.last_page }}</a>
        </li>

        <li v-bind:class="[{disabled: !pagination.next_page_url}]" class="page-item">
        <a class="page-link" href="#" 
        @click.prevent="fetchTickets(pagination.next_page_url)">Next</a>
        </li>
    </ul>
    </nav>

    <div class="input-group input-group-sm mb-2">
    <div class="input-group-prepend">
        <span class="input-group-text" id="inputGroup-sizing-default">Search</span>
    </div>
    <input type="text" class="form-control" v-model="searchItem">
    </div>

    <table>
    <tbody>
        <tr v-for="ticket in filteredTickets" v-bind:key="ticket.auto_id">
        <td>{{ ticket.ticket_id }}</td>
        <td>{{ ticket.category_name }}</td>
        </tr>
    </tbody>
    </table>
</div>
</template>

The corresponding JavaScript code can be seen below:

export default {
    data () {
        return {
            tickets: [],
            ticket: {
                auto_id: '',
                ticket_id: '',
                category_name: '',
                sub_category_name: '',
                message: '',
                filed_date: '',
                is_pin: '',
                is_starred: '',
                severity_name: '',
                est_due_date: '',
                status_name: '',
                filed_by: '',
                assigned_to: ''
            },
            pagination: {},
            searchItem: '',
        }

    },

    created () {
        this.fetchTickets();
    },

    computed: {
        filteredTickets: function () {
            return this.tickets.filter((ticket) => {
                return ticket.ticket_id.match(this.searchItem)
                    || ticket.category_name.match(this.searchItem)
            });
        }
    },

    methods: {
        fetchTickets (page_url) {
            let vm = this;
            page_url = page_url || '/api/tickets';

            fetch(page_url)
                .then(res => res.json())
                .then(res => {
                    this.tickets = res.data;
                    vm.makePagination(res);
                })
                .catch(err => console.log(err));

        },

        makePagination (res) {
            let pagination = {
                current_page: res.current_page,
                last_page: res.last_page,
                next_page_url: res.next_page_url,
                prev_page_url: res.prev_page_url
            }

            this.pagination = pagination;
        }
    }
}

Answer №1

When you use the code this.tickets = res.data;, keep in mind that this.tickets might not necessarily be an array.

In cases where res.data === undefined or res.data === null, invoking this.tickets.filter will result in an error as it is not a function.

To address this issue, consider implementing the following:

if (Array.isArray(this.tickets)) {
  this.tickets.filter(...)
} else {
  // handle the situation appropriately
}

Answer №2

within your code:

data() { 
   return {...

experiment with modifying the

tickets: [],

by updating it to:

tickets: {
       filter: '',
    }

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

Leverage variables in Ajax to retrieve the data of an HTML element

Seeking assistance in converting multiple lines into a single for loop. var optie1 = ""; if($('#form #optie1_check').is(':checked')) { optie1 = $('#form #optie1_naam').val(); } var optie2 = ""; if($('#form #optie2_ch ...

The issue that arises is that only the initial button is able to successfully trigger the opening of

My goal is to create a forum where users can reply to posts by clicking on a "Reply" button that triggers a Bootstrap modal. However, I am facing an issue where the modal only opens when clicking on the first button in a row due to it being placed within a ...

Tips for transferring an object from data attributes to methods within a VueJS component

Check out this complete example first: https://jsfiddle.net/3bzzpajh/ I'm facing a challenge where I want to pass the entire person object to the method showSelectedData. However, when I try to attach the person object to a data attribute like :data- ...

Tips for invoking the ajax pagination feature

Although I have successfully displayed the desired data based on the select box, my pagination feature seems to be malfunctioning. Why is that? The index.php display with broken pagination This snippet shows my ajax script for loading data and setting up ...

PHP - implement a button that triggers an AJAX request to switch languages

I am trying to find a basic PHP script that can modify a variable when a button is clicked to display different languages. Can anyone help me with this? Click on the button to change language options: English - value= en French - value= fr When I click ...

What is the best way to extract row data from a datatable and transmit it in JSON format?

I have successfully created a code that retrieves data from a dynamic table using ajax. However, I am facing an issue where I only want to send data from checked rows. Despite trying different approaches, I keep encountering errors or receive an empty arra ...

In JavaScript, not all elements are removed in a single iteration, even if the condition is consistently met

I'm currently attempting to compare two arrays containing multiple objects and remove elements based on a condition. To my surprise, while testing in Chrome's console, I noticed that the first array (arr1) is not being emptied even though I am us ...

Learn the process of integrating content into a tinymce editor using vue js

Is there a way I can use a button in the vue js template to insert content like <span class="some-class">text</span> inside tinymce editor with the help of the tinymce-vue wrapper? Here is the code snippet: <template> <tiny ...

An element generated using a JavaScript loop is covering another element in the layout

I am facing an issue with positioning images within a div to a span. The problem arises as the images are overlapping each other and I am uncertain about how to properly place each image when it is added. Below is the code snippet: The CSS: <style ty ...

Mongoose encountered an error when attempting to cast the value "......" as an ObjectId in the "author" path. The error was caused by a BSONError

I'm currently facing an issue with Mongoose in my NextJS project. Specifically, I am encountering a problem when trying to save a document where one of the fields references an ObjectId. The error message I receive is as follows: Cast to ObjectId fail ...

Trouble with Ajax requests firing on document load in Firefox

When loading a JSP page in Firefox, I am invoking an AJAX function that calls a servlet. The servlet returns data in response. However, when I attempt to alert the data as shown in the code snippet below, I receive a null value. $.ajax({ url : 'S ...

The filter is displaying incorrect categories

I am facing an issue with creating a work filter based on the last column which represents categories. When I select an option from the dropdown, I want to display only that specific category and hide the others. Currently, when I try clicking on an option ...

Executing a cURL request using Node.js

Looking for assistance in converting the request below: curl -F <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a777f7e737b275a73777b7d7f34706a7d">[email protected]</a> <url> to an axios request if possible. ...

Crafting 3 intertwined combinations using the power of jQuery AJAX and PHP

Here's what I've been working on so far: The first page retrieves data and populates the first combobox, which is all good. Then, when users select a value from combo1, a second combobox is created with filtered data using AJAX - also working fin ...

Handling JSON Objects with Next.js and TypeScript

Currently, I am working on a personal project using Next.js and Typescript. Within the hello.ts file that is included with the app by default, I have added a JSON file. However, I am facing difficulties in mapping the JSON data and rendering its content. T ...

Manipulating the DOM with Javascript and jQuery: Adding a new element and retrieving its reference

I am encountering an issue with a Web App that relies on JavaScript and jQuery. Essentially, the website includes a button that triggers a JavaScript function when clicked. Within this function, there are two other functions named Foo and Bar. Foo generate ...

The node-transmission package seems to be malfunctioning

Recently, I attempted to install a package from this GitHub repository: https://github.com/FLYBYME/node-transmission on my local Node.js setup. However, upon running the example.js file provided in the repository, I encountered the following error: Error: ...

React's setState is not reflecting the changes made to the reduced array

I am currently working on a custom component that consists of two select lists with buttons to move options from the available list to the selected list. The issue I am facing is that even though the elements are successfully added to the target list, they ...

How can I replace specific text with HTML code in a webpage?

In my application, users have the ability to write comments that may contain HTML code. However, this code is escaped before being displayed: <div class="card-body"> <p class="card-text"> &lt;h1&gt;HOLA&lt;/h1&gt; Cita:#2&am ...

Locate a JQuery element within another JQuery element

Apologies for my poor grasp of English. I am working with HTML and JavaScript. <noindex> <h1>This is h1 in noindex</h1> <div> <h1>This is h1 in noindex and in div</h1> <div> <h1>This is h1 in noindex a ...