Consultation with ionic and firebase

0

I am trying to make a query in firebase with ionic and am having the following error:

Thefontlookslikethis:

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

importfirebasefrom'firebase'import{RequestModel}from'../model/order.model'

@Injectable()exportclassRequestService{

//DATA_URL=firebase.storage.StringFormat.DATA_URLpedidos:anypedidosPes:anyconstructor(){//InitializeFirebaseconstconfig={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;
}

}

    
asked by anonymous 12.07.2018 / 05:25

1 answer

0

This happens because you are calling this within a function. Include a variable that receives the contents of this before the function and use this variable in getItems. Try changing the getItens function 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;
}
    
13.07.2018 / 02:36