javascript - React-Native Firebase update column - Stack Overflow

admin2025-04-10  0

In Firebase database, I am trying to update one column only. Firebase database 'users' JSON:

 "users" : {
       "5yty2c8TyChQu3fMJ1O3d44wuq2" : {
       "Email" : "[email protected]",
       "FirstName" : "John",
       "LastName" : "Smith",
       "MobileNumber" : "",
       "NickName" : "",
       "profile_picture" : ""
  }

I would like to update just one column - MobileNumber with this code:

   updateMobileNumber(){

    var updateData = {
                    MobileNumber: this.state.mobileNumber
                      };

    var updates = {};
    updates['/users/' + userId ] = updateData;

    database.ref('users/' + userId).update(updates);

    alert('Account Updated');

   }

The above code instead of updating MobileNumber column, it is removing all the other columns and adding Email column. What am I doing wrong ? I want to update Email column without touching other columns. Appreciate any help.

In Firebase database, I am trying to update one column only. Firebase database 'users' JSON:

 "users" : {
       "5yty2c8TyChQu3fMJ1O3d44wuq2" : {
       "Email" : "[email protected]",
       "FirstName" : "John",
       "LastName" : "Smith",
       "MobileNumber" : "",
       "NickName" : "",
       "profile_picture" : ""
  }

I would like to update just one column - MobileNumber with this code:

   updateMobileNumber(){

    var updateData = {
                    MobileNumber: this.state.mobileNumber
                      };

    var updates = {};
    updates['/users/' + userId ] = updateData;

    database.ref('users/' + userId).update(updates);

    alert('Account Updated');

   }

The above code instead of updating MobileNumber column, it is removing all the other columns and adding Email column. What am I doing wrong ? I want to update Email column without touching other columns. Appreciate any help.

Share Improve this question edited Aug 8, 2017 at 23:28 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Aug 8, 2017 at 23:08 CNK2343CNK2343 1692 gold badges3 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

When you use

updates['/users/' + userId ] = updateData;

you're actually setting the value of users/userId into updateData, which is only 1 field. What you want to use is the method update that allow you to selectively update each field. Like this:

var adaNameRef = firebase.database().ref('users/ada/name');
// Modify the 'first' and 'last' properties, but leave other data at
// adaNameRef unchanged.
adaNameRef.update({ first: 'Ada', last: 'Lovelace' });

Here is the doc to the method: https://firebase.google./docs/reference/js/firebase.database.Reference#update

Also your code can simply be like this:

database.ref('users/' + userId).update({MobileNumber: newNumber});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744274696a239189.html

最新回复(0)