Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Cannot access locals from +page.server.js

$
0
0

I'm following this tutorial https://www.youtube.com/watch?v=doDKaKDvB30 on how to make a login/registration feature with sveltekit and pocketbase.

Here's what the hooks.server.js looks like:

import PocketBase from 'pocketbase';/** @type {import('@sveltejs/kit').Handle} */export const handle = async ({event, resolve}) => {    event.locals.pb = new PocketBase('http://localhost:8090');    event.locals.pb.authStore.loadFromCookie(event.request.headers.get('cookie') || '');    if(event.locals.pb.authStore.isValid) {        event.locals.user = event.locals.pb.authStore.model;    }    const response = await resolve(event);    response.headers.set('set-cookie', event.locals.pb.authStore.exportToCookie({secure: false}));    return response;}

and the +page.server.js for the register screen:

import { redirect } from '@sveltejs/kit';/** @type {import('./$types').Actions} */export const actions = {    register: async ({ locals, request }) => {        const formData = await request.formData();                const data = Object.fromEntries([...formData]);                try {            const newUser = await locals.pb.collection('users').create(data);                    const {token, user} = await               locals.pb.collection('users').authWithPassword(data.email, data.password);        }        catch(err) {            console.log("Error: " + err);            return {                error: true,                message: err            }        }                throw redirect(303, "/");        },};

But when I try to register a new user I get 2 errors:

TypeError: Cannot read properties of undefined (reading 'collection')Data returned from action inside /register is not serializable: Cannot stringify arbitrary non-POJOs (data..message)

I think that is because the data in locals is not being sent to +page.server.js, but I don't know why. That's how it is done in the tutorial and it works!


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>