Cannot set property 'id' of null

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?

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

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

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

Same error: Cannot set property ‘id’ of null

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.

Same issue, different code:

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

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!!

Cannot set property 'currentBill' of null

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

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

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

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

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

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

You have FirebaseListObservable listed as a function?

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

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

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

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.

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