【问题标题】:Firebase dynamic object keyFirebase 动态对象键
【发布时间】:2016-06-14 22:48:19
【问题描述】:

所以我希望我的 firebase 数据库中的密钥是动态生成的。

目前,我有这样的事情

  whatever.$add({
      title: $scope.formData.title
   })

更新:whatever 实际上是 $firebaseArray,是的,返回一个 id。

实际上,在上面的例子中,我想做这样的事情:

  whatever.$add({
      $scope.formData.type: $scope.formData.title
    })

我基本上希望将键设置为来自表单的东西。有办法吗?

【问题讨论】:

    标签: firebase angularfire


    【解决方案1】:

    我假设whatever 实际上是$firebaseArray,如果我是对的,您的$add 将始终生成一个具有随机ID 的新孩子。

    如果您想创建一个具有自定义 id 的新孩子,您应该使用 .child().set()

       var ref = new Firebase(yourFirebaseUrl);
       ref.child(customId).set({
          type: $scope.formData.title
       });
    

    更新:

    要将$scope.formData.title 作为 id,您应该这样做:

       var ref = new Firebase(yourFirebaseUrl);
       ref.child($scope.formData.title).set({
          type: $scope.formData.title
          anotherData: $scope.formData.anotherFormData
       });
    

    【讨论】:

    • 好的,使用$scope.formData.type会报错,create.controller.js:37 Uncaught SyntaxError: Unexpected token .指的是$scope.formData.type中的.
    • @Rexford 如果您希望 $scope.formData.type 成为 id,您应该将 customId 替换为 $scope.formData.type
    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 2020-10-11
    • 2016-03-18
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多