1

I am trying to update one field in my firebase db from Javascript. The code that I used is:

var config = {
                apiKey: "xxxxxxxxxxxxxxx",
                authDomain: "yyyyyyyyy",
                databaseURL: "https://yuyuyu.firebaseio.com/",
                projectId: "demo1233xyz",
              };
              firebase.initializeApp(config);
              var database = firebase.database();

              database.ref("-UserTable/-LGDHSJSKSIUSN178").update({ verified: false });

But this creates a new entry in the firebase instead of updating the same record. Please find below a sample screenshot.

enter image description here

Can somebody help me to understand how to update an existing field in the table through Javascript.

5
  • I don't understand the contents of your "actual outcome" box. It looks like you have two outcomes listed there. Commented Jun 15, 2018 at 19:10
  • What is happening is, the expected change is saved as a new record and the "verified: false" is not getting updated to "verified: true", instead it is added as a new record. Commented Jun 15, 2018 at 19:11
  • But you can't have two children under the same node with the same name. That's impossible. You probably constructed the path incorrectly. It might help if you show a screenshot of the console so it's absolutely clear what you're seeing. Commented Jun 15, 2018 at 19:21
  • Hello Doug, I have updated the screenshot with actual image. Somehow, I am not able to get the specific field updated. Commented Jun 15, 2018 at 19:29
  • be sure and .trim any whitespace from your variables Commented Jun 15, 2018 at 19:33

2 Answers 2

1

Something has an incorrect path. One of the keys starts with a dash character, and the other does not. Of course, you need to be using the same key in both places.

Sign up to request clarification or add additional context in comments.

Comments

0

The best way to do that is

var query = db.ref("<TABLE_NAME>").orderByChild("<FIELD>").equalTo(<VALUE>);
                 query.once("child_added", function(snapshot) {
                   snapshot.ref.update({ <FIELD>: <VALUE> })
                 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.