渲染错误:.. filter不是函数

时间:2019-01-30 03:27:36

标签: javascript vue.js vuejs2

搜索过滤器和页面在第一次加载时正确运行。

每当我单击到下一页时,都会出现错误 **'Error in render: "TypeError: this.tickets.filter is not a function"'**

Tickets.vue:

<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>

javascript:

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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在您的:

data() { 
   return {...

尝试更改您的

tickets: [],

收件人:

tickets: { filter: '', }

答案 1 :(得分:0)

断言this.tickets = res.data;时,this.tickets可能不是数组。

假设{。{1}}或res.data === undefined,this.tickets.filter不是函数。

尝试一下:

res.data === null