tomxor
Blank array of documents from Firestore - createEffect(), createSignal()
Hi all, I'm starting with web frameworks for the first time and chose SolidJS. I'm stuck... retrieving collection of docs with onSnapshot from firestore in createEffect. When the array of docs is populated I'm saving it in variable (createSignal([]) - default value empty array). When I'm using <For each> everything is working, but whenever I want to access an attribute of the first array element (without <For each>) I'm getting error "Cannot read properties of undefined". When debugging I see that the console log is running 2 times - first time the array is empty and second time it has my data. It seems obvious, that when the array is empty for the first time and I want to access the first element it's undefined, so the attribute cannot be retrieved, that's why this error. Is there any nice/standard way to solve this?
I've tried to set up some dummy object for the createSignal default value like
createSignal([{round_nr: 0}])
and this is working, because for the firt time when the function is running the array has this dummy object and is not more undefined and I can read the round_nr attribute which is 0 for the first time and immediately is switched with proper value when the function runs for the second time.
But is it the solution to my problem? Or perhaps is there a better approach?8 replies