adriancarriger / angularfire2-offline

🔌 A simple wrapper for AngularFire2 to read and write to Firebase while offline, even after a complete refresh.
https://angularfire2-offline.firebaseapp.com/
MIT License
207 stars 48 forks source link

Observing single value in DB: No update on change #45

Closed JaneDawson closed 7 years ago

JaneDawson commented 7 years ago

When I subscribe to a single value in my Firebase DB, the observable doesn't trigger when the value changes.

In the following example the "balance" node is of type "Number".

getBalanceStream(userId){
    this.db.object('/users/'+userId+'/balance').subscribe( bal=> {
      this.balance= bal.$value;
    }
  }

When the user's balance changes in the database, the arrow function is not triggered. this.balance doesn't get updated.

When I use regular AngularFire2 it works as expected.

adriancarriger commented 7 years ago

I've been able to reproduce your issue here with a demo.

If you click the button, the data is saved and written to Firebase, but it is not displayed in the ui.

I'll be working on this one. Thanks for posting!

JaneDawson commented 7 years ago

Thank you! I appreciate that.

adriancarriger commented 7 years ago

This should be fixed in version 4.1.5 ..let me know if you still experience any issues. Thanks!

JaneDawson commented 7 years ago

Thank you for updating. I just tested it. Unfortunately it's still the same as before. Observing a single value doesn't work properly and sticks to the initial value.

adriancarriger commented 7 years ago

Hi @JaneDawson, here's a working demo (source code) using angularfire2-offline version 4.1.5

When I was making the demo my package.json had "angularfire2-offline": "^4.1.5" which resolved to version 4.2.0 (a version that was published unintentionally). By changing the package.json to "angularfire2-offline": "4.1.5" the issue was fixed.

You can check which version your app is running by going to:

your-project/node_modules/angularfire2-offline/package.json

Let me know if this solves your issue. Thanks!

JaneDawson commented 7 years ago

That fixed it for me, as well. Works just fine now. Thank you, @adriancarriger. I appreciate it!

adriancarriger commented 7 years ago

Awesome, glad it's working now! 👍

JaneDawson commented 7 years ago

Unfortunately, I have to reopen this issue. Everything works fine, except if the value of the subscribed attribute is 0.

If in the following example the account balance object ..users/'userId'/balance' is 0 in the database, then bal.$value becomes undefined. This shouldn't be, since 0 is a valid value for the balance.

getBalanceStream(userId){
    this.db.object('/users/'+userId+'/balance').subscribe( bal=> {
      this.balance= bal.$value;
    }
  }
JaneDawson commented 7 years ago

Anything new here?

adriancarriger commented 7 years ago

Hi @JaneDawson, thanks for reopening! I'll be looking into this!

adriancarriger commented 7 years ago

I can confirm this issue and have made a demo (with code). When the counter gets to zero no number is shown, however the value in the database is zero. I'll be working fixing this!

JaneDawson commented 7 years ago

Thank you. I appreciate that. Works well now!

adriancarriger commented 7 years ago

Awesome, glad it's working! 👍