My VueJs3 application has a search bar implemented using .filter()
, and it seems to be working fine. However, when I try to pass the value from my methods to the template, an error occurs. My data becomes a proxy and I am unable to use it in that format.
Here is the code snippet:
<template>
<div class="searchBar">
<input type="search" v-on:change="filterList" />
{{ search }}
</div>
<div id="listaDestaque" class="list">
<div class="list-header">
<p>Imagem</p>
<p>Descrição</p>
<p>Categoria</p>
<p>Un</p>
<p>Estoque</p>
<p>Valor</p>
</div>
<div v-for="item in current_data">
<div class="item-list">
<img v-bind:src="item.attributes.image" class="item-image" />
<p>{{ item.attributes.name }}</p>
<p>{{ item.attributes['category-name'].name }}</p>
<p>{{ item.attributes['unit-name'].name }}</p>
<p>{{ item.attributes['quantity-in-stock'] }}</p>
<p>{{ item.attributes.price }}</p>
</div>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import api from '../../services/axios.js';
import headers from '../../services/headers.js';
export default defineComponent({
name: 'listaDestaque',
data() {
return { myList: [], search: '', filter: '', current_data: '' };
},
beforeMount() {
api.get('/products/all', headers).then((response) => {
this.myList = response.data.data;
const filter = this.myList.filter((element) => element.attributes.highlight == true);
this.current_data = filter;
});
},
methods: {
filterList() {
this.$data.search = event.target.value;
console.log(this.search);
const myListArray = JSON.parse(JSON.stringify(this.myList));
const filtered = myListArray.find(
(element) => element.attributes.name == this.search
);
const filteredArray = JSON.parse(JSON.stringify(filtered));
if (filtered) {
this.current_data = filteredArray;
console.log(this.current_data);
console.log(filteredArray);
}
},
},
});
</script>
<style scoped></style>
The section related to the search bar is between lines 2-5 and 35-65.
The output of console.log(filteredArray);
on line 64 looks like this:
Proxy {id: '1', type: 'products', attributes: {…}, relationships: {…}}
[[Handler]]: Object
[[Target]]: Object
attributes: {name: 'Small Marble Chair', description: 'Nihil est dignissimos. Quia officia velit. Est aliquid eos.', quantity-in-stock: 12, price: 58.21, highlight: true, …}
id: "1"
relationships: {category: {…}, unit: {…}}
type: "products"
[[Prototype]]: Object
[[IsRevoked]]: false
An error occurs on lines 17-22 after utilizing the function filterList:
listaDestaque.vue:17 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'image')
at listaDestaque.vue:17:42
at renderList (runtime-core.esm-bundler.js:2905:26)
at Proxy._sfc_render (listaDestaque.vue:24:11)
at renderComponentRoot (runtime-core.esm-bundler.js:896:44)
at ReactiveEffect.componentUpdateFn [as fn] (runtime-core.esm-bundler.js:5651:34)
at ReactiveEffect.run (reactivity.esm-bundler.js:185:25)
at instance.update (runtime-core.esm-bundler.js:5694:56)
at callWithErrorHandling (runtime-core.esm-bundler.js:155:36)
at flushJobs (runtime-core.esm-bundler.js:396:17)