when inserting value into firebase database it wont store correctly

26 Views Asked by At

I have been trying to resolve this, with no joy..

I have a text box, when you enter a name into the text box, and press submit it will upload to firebase. Then a spinner in an app will show they names.

this is the code to upload which uploads the input data to the Realtime database.

function InsertData() {
        push(ref(db, "Spinner/" ),{
            Name: enterName.value
        })

I have attached a picture. the code above when I submitted the name 'Matthew' you will see it sores it as a child under the random key. I need it to store inline exactly like the 'test one you can see at the top.I hope this makes sense.

enter image description here

2

There are 2 best solutions below

1
Doug Stevenson On

Firstly, push always creates a node with a random key name. If that's not what you want, then don't use push.

If you want to create a string value only under a random node (and not nested values), then just provide that string instead of an object as you are now.

push(ref(db, "Spinner"), enterName.value)
0
Frank van Puffelen On

As Doug answered, calling push always created a unique parent key. If you don't want that, call set directly:

set(ref(db, "Spinner/" ),{ Name: enterName.value })

If you don't even want the Name property, you can use:

set(ref(db, "Spinner/" ),enterName.value);

If you want to overwrite the Name property, but keep any other properties you may have under Spinner: If you don't even want the Name property, you can use:

update(ref(db, "Spinner/" ),{ Name: enterName.value });

Also see the Firebase documentation on writing data