#Cant call an objects properties

11 messages · Page 1 of 1 (latest)

serene abyss
#

Hey, Im pretty new to coding and am currently working on a school competition project.

This is my code for a login. It does work as intended, but now I want to show content on my dashboard based on the users role.

  import { load } from '../../(dashboard)/dashboard/+layout';

  let email = '';
  let password = '';
  let loggedInUser = null;
  const users = load();

  function login() {
    loggedInUser = Object.values(users).find(user => user.email === email && user.password === password);

    if (loggedInUser) {
      navigate('/dashboard');
      console.log(loggedInUser);
    } else {
      alert('Invalid email or password');
    }
  }```
And this is one of my user Objects.
```      'User1': {
        firstname: 'John',
        lastname: 'Doe',
        email: '[email protected]',
        password: 'userpassword',
        username: 'johndoe',
        role: 'User',
        FSAW: 5,
        FSW: 10,
        FSM: 33,
        FSY: 325,
        wochenstunden: 36,
        FSWP: 10 / 50,
},```

However, after importing the "loggedInUser" variable from my login page which basically stores the logged in user, I cant call its role
As I get an error "Property 'role' does not exist on type 'typeof Page__SvelteComponent_'. ts(2339). I need help to be able to call the role property
```{#if loggedInUser && loggedInUser.role === "Admin"}```

I've tried fixing this for hours now and cant seem to get any closer
I hope the information I provided is helpful enough
viscid zodiac
#

typescript is saying that loggedInUser is not a user object

#

can you show us the source code for load()?

#

@serene abyss

graceful kestrel
#

This does not seem like proper idiomatic svelte code

#

You have a layout file while implies your using sveltekit but you're using a 3rd party lib for programmatic navigation and you're importing a function from your layout?

#

And you should be using stores at the very least to handle state, not importing variables

serene abyss
serene abyss
graceful kestrel
serene abyss