question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update firebase value based on existing value

See original GitHub issue

Hi , After the update i started having problem updating a value from firebase based on the already exisitng value. so let’s take for example the case of quantity of a specific product.

before i was able to do that :

let item$ = this.af.object('/product/' + productId) item$.take(1).subscribe( item=> { if(item.$exist()) item$.update({ quantity : item.quantity + 1}) })

now with the new API i am having trouble getting that item and updating it in 1 command. Am i missing something ? Hope its not a stupid question but really having all in 1 command solves the issue of having to store locally a “quantity” ( 2 commands : getQuantity and updateQuantity )

Thanks,

Chaker

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Damasycommented, Feb 6, 2019

this my code and it works good, but the problem is that product in set method is undefined! @alberthoekstra

const item$ = this.db.object('/shopping-carts/' + cartId + '/items/' + product.key);
item$.snapshotChanges().pipe(take(1)).subscribe(item => {
  if (item.payload.exists()) {
      item$.update({ quantity: item.payload.val().quantity + 1 });
    } else {
      item$.set({product: product, quantity: 1});
    }
});
1reaction
vijaya1407commented, Mar 13, 2019

There is still one error showing i.e. Property ‘quantity’ doesn’t exist on ‘{}’ . To resolve this, you have to change item.payload.val().quantity to item.payload.exportVal().quantity. const item$ = this.getItem(cartId, product.key);

item$.snapshotChanges().pipe(take(1)).subscribe(item => {
  if (item.payload.exists()) {
    item$.update({
      quantity: item.payload.exportVal().quantity + 1
    });
  } else {
    item$.update({product: product, quantity: 1});
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Firebase Realtime Database - Updating Existing Value
I want to insert "Cart" value inside "MCwD6C_-XCZNGWxFLhO" user, "Cart" has "itemName","quantity","itemPrice" fields. I tried a lot of ways ...
Read more >
Read and Write Data on the Web | Firebase Realtime Database
Generally, you should use the value event techniques described above to read data to get notified of updates to the data from the...
Read more >
Update firebase value based on existing value #1342 - GitHub
Hi , After the update i started having problem updating a value from firebase based on the already exisitng value. so let's take...
Read more >
How to Update Data in Firebase Firestore in Android?
Step 1: Creating a new Activity for updating the data · Step 2: Updating our Modal Class where we were storing our data...
Read more >
Firebase V9 Firestore UPDATE Document Data Using ...
You can also update an existing document field with a new value. Let's change the value of Ontario to ON in the province...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found