Consultation with Ionic and firebase

Asked

Viewed 298 times

0

I’m trying to make an appointment at firebase with Ionic and I’m having the following mistake: inserir a descrição da imagem aqui

The source is like this:

import { Injectable } from '@angular/core';

import firebase from 'firebase' import { Pedidomodel } from '.. /model/request.model'

@Injectable() export class Pedidoservice{

//DATA_URL = firebase.storage.StringFormat.DATA_URL

pedidos:any
pedidosPes:any

constructor(){
    // Initialize Firebase
  const config = {
    apiKey: "xx",
    authDomain: "xx",
    databaseURL: "xx",
    projectId: "xx",
    storageBucket: "xxx",
    messagingSenderId: "xx"
  };
  firebase.initializeApp(config);

  let ref = firebase.database().ref('statuspedido');//statuspedido

  this.pedidos = new Array();

  ref.on('value', (dataSnapshot) =>{
     let items = dataSnapshot.val();
     for(let dados in items){
        this.pedidos.push(
            new PedidoModel(
                items[dados].dataEmissao, 
                items[dados].dataAtualizacao, 
                items[dados].vendedor, 
                items[dados].frete, 
                items[dados].transportadora, 
                items[dados].status)
        )
     }
  })
}

loadDados(){
   // console.log(this.pedidos);
    return this.pedidos;
}
getItems(filtro: string){
    this.pedidosPes = new Array();
    let query = firebase.database().ref('statuspedido').orderByChild('status').startAt(filtro);
    query.on('child_added', function(snap) {
        let ped = snap.val();
        this.pedidosPes.push(
            new PedidoModel(
                ped.dataEmissao, 
                ped.dataAtualizacao,
                ped.vendedor,
                ped.frete, 
                ped.transportadora,
                ped.status)
        )
        console.log(ped.dataAtualizacao, ped.dataEmissao);
    });
    return this.pedidosPes;
}

}

1 answer

0


This happens because you are called this within a function. Include a variable that receives the contents of this before the function and use this variable in getItens. Try to change the function getItens to the following:

getItems(filtro: string){
    this.pedidosPes = new Array();
    let that = this;
    let query = firebase.database().ref('statuspedido').orderByChild('status').startAt(filtro);
    query.on('child_added', function(snap) {
        let ped = snap.val();
        that.pedidosPes.push(
            new PedidoModel(
                ped.dataEmissao, 
                ped.dataAtualizacao,
                ped.vendedor,
                ped.frete, 
                ped.transportadora,
                ped.status)
        )
        console.log(ped.dataAtualizacao, ped.dataEmissao);
    });
    return this.pedidosPes;
}

Browser other questions tagged

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