Problems while trying to recover data from firebase Realtime database

Asked

Viewed 381 times

2

I’m doing my TCC using Ionic 4 and firebase Authentication and Realtime Database, for a few days I ended up crashing when I try to recover only one user from the database, when the user data is saved, the ID used and the same of auth, when I tried to bring all users saved in the database worked smoothly, but when trying to bring only 1 user (the user who is logged in) is not working. The mistake I get and the following

Type 'Observable>>' is Missing the following properties from type 'Usuario': name, course, age, telephone, and 2 more.ts(2740)

These are the codes I’m using

That and the service

 import { Injectable } from '@angular/core';
 import { AngularFireDatabase } from '@angular/fire/database';
 import { AngularFireAuth } from '@angular/fire/auth';



 export interface Usuario {
  nome : string;
  curso: string;
  idade : number;
  telefone : number;
  sexo: string;
  tipo: string;
 }


 @Injectable({
  providedIn: 'root'
 })
 export class UsuarioServiceService {


  constructor(private banco : AngularFireDatabase, private authCtrl : 
AngularFireAuth) {
 }
}

This and my Typescript file

import { Component, OnInit } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFireDatabase } from '@angular/fire/database';
import { Usuario } from '../usuario-service.service';
import  'rxjs/add/operator/take';



@Component({
 selector: 'app-usuario-perfil',
 templateUrl: './usuario-perfil.page.html',
 styleUrls: ['./usuario-perfil.page.scss'],
})

export class UsuarioPerfilPage implements OnInit {

  usuario : Usuario = {
    nome: '',
    curso: '',
    idade : null,
    telefone : null,
    sexo: '',
    tipo: '',
  }

  constructor( private banco : AngularFireDatabase, private authCtrl : 
  AngularFireAuth) { }

  ngOnInit() {

  this.authCtrl.authState.take(1).subscribe(data  =>  {
    if(data && data.email && data.uid){
      this.usuario = this.banco.object<Usuario> 
    (`/usuarios/${data.uid}`).snapshotChanges();
      console.log(data.email)
    }
  else{
    console.log('erro');
   }
  });

 }
}

The error happens when I try to put the data I took from the database into the user variable so that I can then use it in HTML to display the data.

I’ve tried to create an Observable, an Angularfireobject and countless other solutions I’ve seen around but nothing worked, someone knows what I can do?

  • Include code instead of screenshots

1 answer

0

experiment like this:

this.banco.database.ref('/usuarios').orderByKey().equalTo(uid)
  .once('value', (snap) => {
    if (snap.val()) {
      console.log(snap.val()[0]);
    } else {
      console.log('erro');
    }
  })

as it is a list I’m picking the record 0 but you could iterate with the object class ex:

const snap  = snap.val();
Object.keys(snap).foreach( key => {
    console.log(snap[key]);
})

if you have any mistakes forgive me because I typed the code here and do not test.

hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.