Cannot set property 'id' of null


#1

Hi,

I need to fetch data from firebase…:

currentBill: any = {};

ionViewDidEnter(){
      this.firebaseData.getBillDetail(this.navParams.get('billId'))
      .on('value', snapshot => {
        this.currentBill = snapshot.val();
        this.currentBill.id = snapshot.key;
      });
    }

The firebase provider has following:

getBillDetail(billId: string): firebase.database.Reference{
  return firebase.database().ref('/Bill' + billId).child(billId);
}

Not sure, what am I missing here?


#2

Add a console.log(this.currentBill) before you set the id - probably this is empty and didn’t work.


#3

The result:

FirebaseListObservable
$ref
:
U
operator
:
ObserveOnOperator
source
:
FirebaseListObservable
$ref
:
U
_isScalar
:
false
_subscribe
:
function (obs)
__proto__
:
Observable
constructor
:
function FirebaseListObservable($ref, subscribe)
lift
:
function (operator)
push
:
function (val)
remove
:
function (item)
update
:
function (item, value)
_checkOperationCases
:
function (item, cases)
__proto__
:
Object
_isScalar
:
false
__proto__
:
Observable

#4

try changing return firebase.database().ref('/Bill' + billId).child(billId)
to return firebase.database().ref('Bill/' + billId).child(billId);


#5

Same error: Cannot set property ‘id’ of null


#6

should this.currentBill be an object? Or a string? Sounds like it may be an accidental object.
also, try ensuring that billId = string;

 return firebase.database().ref('Bill/' + tempId).child(tempId);```
might work.

#7

Same issue, different code:

getBillDetail(billId: string): firebase.database.Reference{
  let tempId = billId.toString();
  return firebase.database().ref('Bill/' + tempId).child(tempId);
}

#8

Also, try this.firebaseData.getBillDetail(this.navParams.get('billId')) .once("value", function(snapshot) { this.currentBill = snapshot.val(); this.currentBill.id = snapshot.key; }

snapshot isn’t wrapped in your original code, and using once instead of on might help out. Not giving everything extremely similar names would totally help too…it’s like a mineField!!


#9
Cannot set property 'currentBill' of null

Also, I am not so sure about “function” and snapshot in your code


#10

it’s working very well on my screen. Add a ) at the end if you try it. I forgot it in my example. You should also be able to access ‘this’, which you can’t do with your previous function. That’s probably why this.currentBill was returning null.currentBill.

If not, make a reference to this.currentBill, like…let curBill = this.currentBill; and try using curBill instead. I literally went through this process for hours overnight. Same exact problems


#11

Missed to mention this.currentBill = angFire.list('/Bills'); Can it help?


#12

You definitely missed that one. Try angFire.list(‘Bills’);


#13

and above constructor, try currentBill: FirebaseListObservable + <any[]>
without the +.


#14

Same error… Had tried these earlier too. Seems something else is missing…


#15

You have FirebaseListObservable listed as a function?


#16

I don’t know man. Your coding is mui different than mine.


#17

No. May be its different… I have used similar logic to get the results earlier… Seems somthing naive here. Lets see


#18

I guess, in my view, I see a ton of functions returning functions and references in order to get
this.currentBill = firebase.database().ref('Bills');
when you could just write
this.currentBill = angFire.list('Bills');
or make a seperate this.items to hold 'Bills’
this.items = angFire.list('Bills');
then work from there


#19

My last suggestion is trying to start from the ground up.

let myConnection =  firebase.database().ref('/');
      myConnection.orderByKey().on("child_added", function(snapshot)    {
       console.log("Got");
          console.log(snapshot.val());
});```

and see what pops up.
and don't use 'any'. It helps very, very much.

#20

Never type the word “function” inside of one. You are begging for execution context loss bugs. Always use arrow functions instead.