【问题标题】:Firebase POST custom IDFirebase POST 自定义 ID
【发布时间】:2017-12-20 03:39:27
【问题描述】:

在使用 POST 方法时,有没有办法在 Firebase 中设置自定义 ID?类似:

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

我正在开发一个 Vue.js 项目并试图拯救一些学生和他们的父母,例如:

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);
    });

【问题讨论】:

    标签: javascript firebase firebase-realtime-database vue.js


    【解决方案1】:

    确保使用 put 而不是 post 请求,否则您最终会在“user_one”中使用自动生成的 id

    POST 会给你:

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

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

    PUT 会给你你想要的。

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

    【讨论】:

    • 不会使用“PUT”而不是“POST”覆盖数据?
    【解决方案2】:

    确实有。只需使用 HTTP PUT 方法并指定整个路径。根据您的图书馆,它可能很简单:

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

    如需了解更多信息,请参阅Firebase REST API documentation on the PUT command

    【讨论】:

      【解决方案3】:

      如果您使用的是 Php cURL,我通过在 url 中添加我的自定义 ID 使用 PUT 实现了这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        • 2018-07-07
        • 2019-03-21
        相关资源
        最近更新 更多