在 Firebase 中使用 push() 如何提取唯一 ID [英] In Firebase when using push() How do I pull the unique ID

查看:19
本文介绍了在 Firebase 中使用 push() 如何提取唯一 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Firebase 数据库中添加/删除条目.我想将它们列在要添加/修改/删除(前端)的表中,但我需要一种方法来唯一标识每个条目以便修改/删除.Firebase 在使用 push() 时默认添加一个唯一标识符,但我在 API 文档中没有看到任何关于如何选择这个唯一标识符的内容.这甚至可以做到吗?我应该使用 set() 来创建唯一 ID 吗?

我已经使用他们的教程将这个快速示例放在一起:

<input type='text' class="td-field" id='nameInput' placeholder='Name'><input type='text' class="td-field" id='messageInput' placeholder='Message'><input type='text' class="td-field" id='categoryInput' placeholder='Category'><input type='text' class="td-field" id='enabledInput' placeholder='Enabled'><input type='text' class="td-field" id='approvedInput' placeholder='Approved'><input type='Button' class="td-field" id='Submit' Value="Revove" onclick="msgRef.remove()"><脚本>var myDataRef = new Firebase('https://unique.firebase.com/');$('.td-field').keypress(function (e) {如果(e.keyCode == 13){var name = $('#nameInput').val();var text = $('#messageInput').val();var category = $('#categoryInput').val();var enabled = $('#enabledInput').val();var批准 = $('#approvedInput').val();myDataRef.push({名称:名称,文本:文本,类别:类别,启用:启用,批准:批准});$('#messageInput').val('');}});myDataRef.on('child_ added', 函数(快照){var message = snapshot.val();displayChatMessage(message.name, message.text, message.category, message.enabled, message.approved);});功能 displayChatMessage(名称,文本,类别,启用,批准,){$('<div/>').text(text).prepend($('<em/>').text(name+' : '+category +' : '+enabled +' : '+批准+ ' : ' )).appendTo($('#messagesDiv'));$('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;};

现在假设我有三行数据:

fred : 1 : 1 : 1 : 测试消息 1fred : 1 : 1 : 1 : 测试消息 2fred : 1 : 1 : 1 : 测试消息 3

如何唯一标识第 2 行?

在 Firebase 数据库中,它们如下所示:

-数据库名称-IuxeSuSiNy6xiahCXa0批准:1"类别:1"启用:1"姓名:弗雷德"文本:测试消息 1"-IuxeTjwWOhV0lyEP5hf批准:1"类别:1"启用:1"姓名:弗雷德"文本:测试消息 2"-IuxeUWgBMTH4Xk9QADM批准:1"类别:1"启用:1"姓名:弗雷德"文本:测试消息 3"

解决方案

要获取任何快照的名称"(在本例中,是由 push() 创建的 ID),只需像这样调用 name() 即可:

var name = snapshot.name();

如果你想得到由 push() 自动生成的名字,你可以在返回的引用上调用 name(),就像这样:

var newRef = myDataRef.push(...);var newID = newRef.name();

<小时>

注意:snapshot.name() 已被弃用.查看其他答案.

I'm attempting to add/remove entries from a Firebase database. I want to list them in a table to be added/modified/removed (front end) but I need a way to uniquely identify each entry in order to modify/remove. Firebase adds a unique identifier by default when using push(), but I didn't see anything referencing how to select this unique identifier in the API documentation. Can this even be done? Should I be using set() instead so I'm creating the unique ID?

I've put this quick example together using their tutorial:

<div id='messagesDiv'></div>
<input type='text' class="td-field" id='nameInput' placeholder='Name'>
<input type='text' class="td-field" id='messageInput' placeholder='Message'>
<input type='text' class="td-field" id='categoryInput' placeholder='Category'>
<input type='text' class="td-field" id='enabledInput' placeholder='Enabled'>
<input type='text' class="td-field" id='approvedInput' placeholder='Approved'>
<input type='Button' class="td-field" id='Submit' Value="Revove" onclick="msgRef.remove()">

<script>
var myDataRef = new Firebase('https://unique.firebase.com/');

  $('.td-field').keypress(function (e) {
    if (e.keyCode == 13) {
      var name     = $('#nameInput').val();
      var text     = $('#messageInput').val();
      var category = $('#categoryInput').val();
      var enabled  = $('#enabledInput').val();
      var approved = $('#approvedInput').val();
      myDataRef.push({name: name, text: text, category: category, enabled: enabled, approved: approved });
      $('#messageInput').val('');
    }
  });
  myDataRef.on('child_added', function(snapshot) {
    var message = snapshot.val();
    displayChatMessage(message.name, message.text, message.category, message.enabled, message.approved);
  });
  function displayChatMessage(name, text, category, enabled, approved, ) {
    $('<div/>').text(text).prepend($('<em/>').text(name+' : '+category +' : '+enabled +' : '+approved+ ' : ' )).appendTo($('#messagesDiv'));
    $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
  };
</script>

Now lets assume I have three rows of data:

fred : 1 : 1 : 1 : test message 1
fred : 1 : 1 : 1 : test message 2
fred : 1 : 1 : 1 : test message 3

How do I go about uniquely identifying row 2?

in the Firebase Database they look like this:

-DatabaseName
    -IuxeSuSiNy6xiahCXa0
        approved: "1"
        category: "1"
        enabled: "1"
        name: "Fred"
        text: "test message 1"
    -IuxeTjwWOhV0lyEP5hf
        approved: "1"
        category: "1"
        enabled: "1"
        name: "Fred"
        text: "test message 2"
    -IuxeUWgBMTH4Xk9QADM
        approved: "1"
        category: "1"
        enabled: "1"
        name: "Fred"
        text: "test message 3"

解决方案

To get the "name" of any snapshot (in this case, the ID created by push()) just call name() like this:

var name = snapshot.name();

If you want to get the name that has been auto-generated by push(), you can just call name() on the returned reference, like so:

var newRef = myDataRef.push(...);
var newID = newRef.name();


NOTE: snapshot.name() has been deprecated. See other answers.

这篇关于在 Firebase 中使用 push() 如何提取唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆