PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 17, 2022

[FIXED] how to update info in firebase version 9

 October 17, 2022     firebase, firebase-realtime-database, javascript, vue.js     No comments   

Issue

I would like besides add new record , also change 'bill'. But I don't know what input in this string

const newPostKey = (child(ref(db), )).key;

This action in vuex, that I use for create request to firebase:

async updateInfo({ dispatch, commit, getters }, toUpdate) {
  try {
    const uid = await dispatch('getUid')
    const db = getDatabase();
    const updateData = { ...getters.info, ...toUpdate }
    const postListRef = ref(db, `/users/${uid}/info`);
    const newPostKey = (child(ref(db), ????)).key;
    const updates = {};
    updates[newPostKey] = updateData;
    update(postListRef, updates)
    commit('setInfo', updateData)

  } catch (e) {
    commit('setError', e)
    throw e
  }
},

Code work up to step const newPostKey..

If I input in this string 'info':

const newPostKey = (child(ref(db), 'info')).key;

Record will be added, but 'info' will be update incorrect: enter image description here


Solution

If you just want to update the value of bill then you can try the following:

const userRef = ref(db, `/users/${uid}/info`);

update(userRef, { bill: 100 }).then(() => { // <-- replace 100 with your bill amount
  console.log("Bill Amount Updated")
}).catch(e => console.log(e))

If you want to increment the bill by a certain amount, you can do so using increment() function.

update(userRef, { bill: increment(100) }) // increment by 100 


Answered By - Dharmaraj
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing