1

this is my code

computed: {
    filterKategori() {
      var kategori = this.kategori.data.filter(f => {
        return f.induk == null && f.id_klasifikasi == this.id_klasifikasi;
      });

      return kategori;
    }
  },

and this is my structure data

kategori: {
    current_page: 1
    data: [{id: "01", nama_kategori: "Karya Umum", reorder: 6, ishide: "0", id_klasifikasi: "01", induk: null},…]
    first_page_url: "http://localhost:8002/api/fetch/kategori?page=1"
    from: 1
    last_page: 1
    last_page_url: "http://localhost:8002/api/fetch/kategori?page=1"
    next_page_url: null
    path: "http://localhost:8002/api/fetch/kategori"
    per_page: "10"
}

can't anyone help me please, why the filter is'nt working and the error says this cannot read property filter of undefined

4
  • 1
    Is data inside kategori? Bc from your json I can't tell exactly. If it's outside you have to do this.data.filter
    – Raul Rene
    Jul 15, 2019 at 9:02
  • yes, the data is inside kategori Jul 15, 2019 at 9:06
  • Where is kategori placed on your Vue elem? It should be in a data: function () { return { kategori: { } } } in order to work
    – Raul Rene
    Jul 15, 2019 at 9:15
  • kategori is placed on : data() { return{ kategori: [] } } @RaulRene Jul 15, 2019 at 9:19

1 Answer 1

0

It seems that in one of the requests you get "null" instead "this.kategori.data" and the filter is called as a property (legacy reasons in ECMAScript more https://2ality.com/2013/10/typeof-null.html) More about "null": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null

u need add additional check on "data" property:

computed: {
    filterKategori() {
      if(!this.kategori.data) return kategori;

      var kategori = this.kategori.data.filter(f => {
        return f.induk == null && f.id_klasifikasi == this.id_klasifikasi;
      });

      return kategori;
    }
  },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.