0

The following code will store an object with au automatically generated name. Instead, I want to be able to specify that name in advance. How would I go about that? Thanks

        const request = new Request('firebaseUsr.json',{
            method:'POST',
            headers:{
                    name:formState.inputValues.username,
            },
            body:JSON.stringify({
                username,
                password

            })
        })

    fetch(request)
    .then(response => {
        if (response.status === 200) {
        return response.json();
        } else {
        throw new Error('Something went wrong on api server!');
        }
    })
    .then(response => {
        console.debug(response);
        // ...
    }).catch(error => {
        console.error(error);
    });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Babou
  • 151
  • 1
  • 12

1 Answers1

1

According to the documentation for the Firebase Realtime Database REST API, a POST is the equivalent of a push operation that uses a random ID. So, if you don't want that, don't use a POST. Instead, you probably want a PUT which allows you to specify the full path of the node to write. You determine that path in the path of the request. So you would specify "/path/to/node.json" in order to update only "/path/to/node".

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Yes, @Doug Stevenson. That is exactly what I was looking for. Thank you very must. Now I can just add my object name as part of my URL and I will get the children of that object to be the different elements I want to store – Babou Jun 15 '20 at 04:43