【问题标题】:How to set a unique ID for each person in a Firebase database so I can choose which to remove if more than one person have the same name?如何为 Firebase 数据库中的每个人设置一个唯一 ID,以便在多个人同名时选择删除哪个 ID?
【发布时间】:2014-11-06 16:10:06
【问题描述】:

我会尽量解释清楚。

我正在制作一个通讯录,您可以在其中存储以下内容:

  • 名字
  • 姓氏
  • 电子邮件
  • 电话号码
  • 没有什么可以阻止您添加两个名字和姓氏相同的人,例如 Tom Sawyer。 现在,假设我们有两个 Tom Sawyer 添加到 Firebase 数据库,在 Firebase 数据库生成的两个随机且唯一的键下。像这样:

    它在 HTML 中显示如下:

    当您单击其中一个名称旁边的删除按钮时,它应该从 HTML 和数据库中消失。不过,这就是棘手的地方,我无法确定应该从数据库中删除哪个 Tom Sawyer,我不能使用唯一的键名,因为如果我遍历它们并搜索 Tom Sawyer,我会得到两次点击只会随机选择一个。

    我的第一个想法是添加第二个唯一 ID 作为属性,但这只会以与唯一 Firebase 密钥 ID 相同的方式失败。唯一 ID 必须与添加的名字和姓氏相关联,而不是随机的(至少在我看来是这样),以便我可以将 HTML Tom Sawyer 与数据库中的 the Tom Sawyer 联系起来。这必须独立发生在数据库中存在多少 Tom Sawyer,否则它将无法工作。

    我应该如何解决这个问题?

    【问题讨论】:

    • 每次添加新联系人时,都将这个全局变量设置为指向它:newChildRef = newChild.name();。这解释了第二段中的行为。但总的来说,我的建议是在你的 removeFromDb 函数中放置一个断点,然后看看 newChildRef 的值是多少。很可能它不是(总是)你期望的那样。
    • 你好 Chrillewoodz!欢迎来到社区。对您的一些想法,因为我无法猜测问题的当前状态:a)不鼓励全局变量是有原因的 b)值被设置为键(使用 .name())但是被称为好像它包含一个 ref c)这里有很多与问题集无关的不必要代码和 d)understand the problem set 有很多必要的缺失代码
    • @Kato 我知道现在的问题是什么,但我仍然不知道如何解决它。它基本上归结为这样一个事实,即您可以在数据库中拥有多个具有相同名字和姓氏的人,但是除了键名之外,我没有一个好的方法可以用唯一的 ID 来分隔这些人。我不能用它来识别谁是谁。因此,据我所知,在我弄清楚如何添加唯一的非随机 ID 之前,我的问题没有解决方案。
    • 缩小您的问题范围以适应新发现,并包含可用于重现该场景的一小段代码示例,我很乐意为您提供帮助。
    • @Kato 我已经修改了整个问题,希望现在更清楚了。如果您想提供帮助,我将不胜感激。

    标签: javascript firebase


    【解决方案1】:

    您想使用 .key()(2.x 之前的 .name())来获取每条记录的推送 ID。

    一个 pojo 示例(错误,+jQuery)

    例如,在 pojo 中(使用 jQuery 来抽象 DOM 位):

    var ref = new Firebase(URL);
    ref.child('contacts').on('child_added', function(snap) {
       var data = snap.val();
       var name = data.first + ' ' + data.last;
    
       // some proprietary code to add them to the DOM, the critical part 
       // here being that we include the push id in the data...
    
       // set the push id as data-id so we can refer to it!
       var html = '<li data-id="' + snap.key() + '">' +
           '<span>' + name + '</span> ' +                  // user's name
           '<button data-event="edit">Edit</button> ' +    // edit button
           '<button data-event="delete">Delete</button>' + // delete button
           '</li>';
       $(html).appendTo('#theList');
    });
    
    // handle clicking the delete button
    $('[data-event="delete"]').click(function(e) {
       e.preventDefault();
    
       // fetch the push id from data-id="..." attribute
       var pushId = $(this).closest('li').attr('data-id');
    
       // remove it from Firebase
       ref.child('contacts').child(pushId).remove();
    });
    

    框架示例(Angular)

    当然,一个好的框架可以为你节省大量的精力。例如,我是 Angular 的粉丝:

    <ul>
      <li ng-repeat="user in contacts">
         <span>{{user.first}} {{user.last}}</span>
         <button>Edit</button>
         <button ng-click="users.$remove(user)">Delete</button>
      </li>
    </ul>
    

    我的 Angular 控制器:

    app.controller('ctrl', function($scope, $firebase, $window) {
       var ref = new $window.Firebase(URL);
       $scope.contacts = $firebase(ref).$asArray();
    });
    

    【讨论】:

    • 事情是我不想在列表项上重复,我为字母表中的每个字母显示一个 li,然后根据姓氏在相应类别下附加名称以。。开始。所以我不确定我会在 ng-click 中放入我拥有的结构吗?
    • 这是一个与如何为每个人设置唯一 ID 完全不同的问题。完全不同。请参阅这篇文章,其中解释了如何创建具有多个嵌套数据层的指令:stackoverflow.com/questions/26047958/…
    【解决方案2】:

    在那里,您可以将 firebase 与 angular js 一起使用。这样从 firebase 中编辑和删除数据就容易多了。 使用 Firebase.js,然后使用 Angular.js,然后使用 angularfire.js

    //In JavaScript File,Initialize angularJs like this
    
    var app=angular.module('YourAppName',[firebase])
    //this is dependencies injection you have to do like this
    
    //get the firebase URL and store it like this
    
    var fb_url=new Firebase('Your Firebase database URL') 
    //then store it in $scope variable i.e 
    
    $scope.data=$firebaseArray(fb_url)
    
    //In Html file,
    
    <div>
    <table>
    <tr>
    <th>Name</th>
    <th>Age</th>
    <th>Action</th>
    </tr>
    <tr ng-repeat="da in data">
    <td>{{da.name}} </td>
    <td>{{da.age}} </td>
    <td><input type="button" value="Edit" ng-click="EditBtn(da)"/></td>
    <td><input type="button" value="delete" ng-click="DelBtn(da)"/></td>
    </tr>
    </table>
    </div>
    //In Javascript File,
    
    $scope.EditBtn=function(da){
    $scope.Name=da.name;
    $scope.age=da.age;
    $scope.id=da.$id;
    //this is unique id which generated when you insert the data.
    }
    //In Html
    
    <div>
    <input type="text" ng-model="Name"/>
    <input type="text" ng-model="age"/>
    <input type="button" value="Save Edit" ng-click="SaveEdit()"/>
    </div>
    
    //In Js file    
    
    $scope.SaveEdit=function(){
    var Id=$scope.id;
    var record=$scope.data.$getRecord(Id);
    record.name=$scope.Name;
    record.age=$scope.Age;
    $scope.data.$save(record);
    }
    
    $scope.DelBtn=function(da){
    $scope.data.$remove(da);
    }
    

    【讨论】:

    • 您有更简单的解决方案吗?添加 Angular 只是为了获取条目的唯一 ID,这是相当笨拙的。
    • 嗨,Demurugos。这是在 angularfire 中最简单的方法
    • 但他没有提到 Angularfire 和 Angular
    猜你喜欢
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多