I have a Pinia store:
import { defineStore } from "pinia";
import { api } from "@/services/api";
export const useMaterialStore = defineStore("material", {
state: () => ({
material: {},
}),
actions: {
async load_material(machine_id) {
const response = await api.get("material/machine/" + machine_id);
console.log(response.data);
this.material[machine_id] = response.data;
},
},
getters: {
getReadyMaterial: (state) => {
return (machine_id) => state.material[machine_id];
},
},
});
The load_material action gets an array of objects from the API and stores it to the state.
The state looks like this:
{
"2": [
{
"id": 1,
"machine": 2,
"order_date": null,
"confirm_date": null,
"delivery_date": null,
"ready": false
}
],
"4": [
{
"id": 2,
"machine": 4,
"order_date": null,
"confirm_date": null,
"delivery_date": "2023-10-30",
"ready": true
},
{
"id": 3,
"machine": 4,
"order_date": null,
"confirm_date": null,
"delivery_date": null,
"ready": false
}
]
}
This is my component:
<template>
<div>Ready: {{ materialStore.getReadyMaterial(props.machine_id) }}</div>
</template>
<script setup>
import { onMounted } from "vue";
import { useMaterialStore } from "@/store/material";
const materialStore = useMaterialStore();
const props = defineProps({
machine_id: Number,
});
onMounted(() => {
materialStore.load_material(props.machine_id);
});
</script>
All this works as expected, if props.machine_id is 2, the div looks like this:
[ { "id": 1, "machine": 2, "order_date": null, "confirm_date": null, "delivery_date": null, "ready": false } ]
No comes the problem, I want to filter the array for objects that have the ready set to true.
If I change the getter to return (machine_id) => state.material[machine_id].filter((m) => m.ready == true); I get an error in the console:
Uncaught (in promise) TypeError: state.material[machine_id] is undefined
I'm completely lost whats going on here and hope somebody can point out what I'm doing wrong