xnok
xnok

Reputation: 329

Session value returns null when checking whats stored

I am working on an ionic app, that i need to create a session, so when the user logins, i have his name stored, then i can check if the user really is logged in or not. To do so, i create a model class and a user class. My issue is that when i try to console log my value stored inside ionic it returns null values, here is an image explaining exactly the output:

enter image description here

I don't understand why is it logging like '0: 'n', 1: 'o', 2: 'm', 3: 'e'. What I want to do is store the username inside my session and log at the same time.

Here's how i am doing this:

import { Storage } from "@ionic/storage";
import { Injectable } from '@angular/core';
import { Usuario } from "./interface/usuario";

@Injectable()
export class Session {

    constructor(public storage: Storage){}

    create(usuario: Usuario) {
        this.storage.set('usuario', usuario);
    }

    get(): Promise<any> {
        return this.storage.get('usuario');
    }

    remove() {
        this.storage.remove('usuario');
    }

    exist() {
        this.get().then(res => {
            console.log('resultado >>> ', res);
            if(res) {
                console.log('resultado IF');
                return true;
            } else {
                console.log('resultado else');
                return false;
            }
        });
    }
}

My User/Usuario class:

import { Model } from './../models/model';

export class Usuario extends Model{
    nome: string;
    email: string;
    login: string;
    senha: string;
} 

Model Class

import { Usuario } from './../interface/usuario'; 

export class Model{ 
    constructor(Usuario){ 
        Object.assign(this, Usuario); 
    } 
} 

Where I retrieve what was stored inside my Usuario object and log it right after

ionViewDidLoad() {
  this.carregarPerfil();
  this.session.get()
    .then(res => {
      this.usuarioLogado = new Usuario(res);
      console.log('usuário logado  >>> ', this.usuarioLogado);
    });

  console.log(this.session.exist());

  this.session.remove();

  this.session.get()
    .then(res => {
      this.usuarioLogado = new Usuario(res);
      console.log('USUARIO LOGADO  >>> ', this.usuarioLogado);
    });

  console.log(this.session.exist());
}

EDIT: here's what happens when i console.log inside create, using storage.get

https://imgur.com/a/W2rQhd8

    create(usuario: Usuario) {
        this.storage.set('usuario', usuario);
        console.log('[TEST] VALUE BEING PASSED IS ', this.storage.get('usuario'));
    }

Upvotes: 0

Views: 463

Answers (1)

Daniel Gimenez
Daniel Gimenez

Reputation: 20534

This is what's going on:

  • In login.ts you have the following line: this.usuario = new Usuario('nome');
  • Usuario inherits from Model.
  • Model calls object.assign() using the parameter from the constructor to set its own parameters.
  • A string is inherently an array.
  • So by calling object.assign(this, 'nome') you are creating parameters corresponding to each index in the string with the value as each character.

Obviously you want to do something different on the line, such as passing item into the constructor. However I think the object.assign business is asking for trouble and you should be much more explicit about the deserialization of your result.

export class Usuario {
    nome: string;
    email: string;
    login: string;
    senha: string;

    constructor(src: any) {
        if (src) {
            this.nome = src['nome'] || undefined;
            this.email = src['email'] || undefined;
            this.login = src['login'] || undefined;
            this.senha = src['senha'] || undefined;
        }
    }
}

Upvotes: 1

Related Questions