'Use Supabase .select with Sveltekit store
if in my current .svelte file i have
let todos = [];
onMount(async () => {
let { data, error } = await supabase .from('todos') .select('*')
todos = data;
however i don't think that's pretty efficient,
in my store.js file, should it be written as
export const todos = writable([])
export const loadTodos = async() => {
let { data, error } = await supabase
.from('todos')
.select('*')
todos = data;
}
Im not sure if that is even correct but, If so, how do i call it back into my .svelte file to load ?
Solution 1:[1]
Got the answer for it
import { writable } from 'svelte/store';
import supabase from '$lib/db';
export const todos = writable([]);
export const getTodos = async () => {
let { data, error } = await supabase.from('todos').select('*');
if (!error) {
todos.set(data);
}
};
and in the svelte file.
<script>
import { onMount } from 'svelte';
import { todos, getTodos } from '$lib/stores/test';
getTodos();
</script>
<div>
{#each $todos as task}
{task.task}
{/each}
</div>
now to figure out how to delete and update
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jon G Stødle |
