0

is there a way to set a custom ID in Firebase when using the POST method? something like:

{
  "users": {
    "user_one": {
      "username": "jdoe",
      "password": "123"
    }
  }
}

I'm working on a Vue.js project and trying to save some students and their parents like:

let padre = {
  nombre: this.padre.nombre,
  apellido: this.padre.apellido,
  cedula: this.padre.cedula,
  nacionalidad: this.padre.nacionalidad,
  estado_civil: this.padre.estado_civil,
  instruccion: this.padre.instruccion,
  profesion: this.padre.profesion,
  trabaja: this.padre.trabaja,
  l_trabajo: this.padre.l_trabajo,
  d_trabajo: this.padre.d_trabajo,
  tlf_trabajo: this.padre.tlf_trabajo,
  tlf_habitacion: this.padre.tlf_habitacion,
  tlf_movil: this.padre.tlf_movil
}

  axios.post('https://projectname.firebaseio.com/padres.json', padre)
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gustavo Ordaz
  • 57
  • 1
  • 9

3 Answers3

2

Make sure to use the put instead of the post request else you'll end up with the auto generated id inside of your "user_one"

POST will give you:

{
  "users": {
    "user_one": {
      "134134hnj34nuj134bn": {
        "username": "jdoe",
        "password": "123"
      }
    }
  }
}

axios.put('https://projectname.firebaseio.com/padres/user_one.json', padre)

PUT will give you what you want.

{
  "users": {
    "user_one": {
      "username": "jdoe",
      "password": "123"
    }
  }
}
Bram G
  • 183
  • 1
  • 11
0

There sure is. Just use the HTTP PUT method and specify the entire path. Depending on your library it may be as simple as:

axios.post('https://projectname.firebaseio.com/padres/user_one.json', padre)

For more, see the Firebase REST API documentation on the PUT command.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

If you are using Php cURL, i had achieved this with PUT by adding my custom ID in url.

Chirag Joshi
  • 409
  • 1
  • 4
  • 13