0

I have an object containing multiple key value pairs, I want to add all the keys and their values, from inside the object to an existing node, without disturbing the data already present inside the node.

If i write like this

var ref = firebase.database().ref("hams/spam_words/");

    ref.update({   

       new_words_ham //new_word_ham is an object containing n number of words

    });

it will add new_words_ham as another child node inside the main node , i cannot have that

even using a forloop on the object does not work

var ref = firebase.database().ref("hams/spam_words/");
    for(var i in new_words_ham){
        var word = i
    ref.update({   
      i
    });

I am new to js as well as to firebase. Please do tell me if i have got any concept wrong

4
  • also can some one just tell me if i am using the terms node and child in the wrong sense here Commented Jan 3, 2019 at 6:56
  • This statement looks a bit strange ref.update({ new_words_ham }); Are the wrapping curly brackets a typo or is removing them solving your problem? Commented Jan 3, 2019 at 7:33
  • 1
    @DennisAlund modern JavaScript allows { new_words_ham } as a shorthand for { new_words_ham: new_words_ham } . Yes, I find it incredibly hard to get used to too. ;-) Commented Jan 3, 2019 at 14:45
  • Thanks for that, I learned something interesting from this 😊 Commented Jan 4, 2019 at 0:30

1 Answer 1

1

Your existing code

//new_word_ham is an object containing n number of words
firebase.database().ref("hams/spam_words/").update({   
   new_words_ham 
});

Can be rewritten as

firebase.database().ref("hams/spam_words/").update({   
   new_words_ham: new_words_ham
});

when the shorthand syntax is expanded. What I believe you want is simply

firebase.database().ref("hams/spam_words/").update(new_words_ham);
Sign up to request clarification or add additional context in comments.

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.