【问题标题】:Remove item using its location or key in Firebase React使用其在 Firebase React 中的位置或键删除项目
【发布时间】:2017-07-29 07:20:15
【问题描述】:

我有一个简单的反应应用程序,我可以向其中添加数据,但我不确定如何删除/更新数据。 主要问题在于获得我告诉firebase要删除哪些数据的部分。我怎么告诉火力基地。 我正在使用反应。

我一直在尝试不同的东西,但它不起作用

handleRemove(){
  console.log('you reached handleRemove function');
  var ref =firebase.database().ref('items');
  ref.on('value',this.handlegetData,this.handleErrData);
  ['items']['KpAmo20xP6HPXc7cwjY'].remove();
  //itemsRef.remove('KpAmo20xP6HPXc7cwjY');
}

请告诉我该怎么做。

我的 firebase 数据库看起来有点像这样

【问题讨论】:

  • 您的按键真的以破折号/减号字符开头吗?这令人困惑。

标签: reactjs firebase firebase-realtime-database


【解决方案1】:

你需要这样的东西来消除价值:

handleRemove() {
    return firebase.database().ref('items').child('ITEM_KEY').remove();
}

或类似的东西来更新价值:

handleUpdate() {
    var updates = {};
    updates['/id'] = 1;
    updates['/title'] = 'Apple';

    return firebase.database().ref('items').child('ITEM_KEY').update(updates);
}

(在您的屏幕截图中,items 等于 firebase-test

Here the Firebase Realtime Database documentation.

【讨论】:

  • 我试过了,我仍然可以在我的数据库中看到该项目
  • 是的,做到了。
  • 你确定密钥真的存在吗?
  • 好吧,我想知道为什么,您可能忘记了 '-' 字符(密钥的第一个字符)
  • 是的,它确实有效。耶!!谢谢。如果我只想删除标题或用户,你能告诉我该怎么做吗?
猜你喜欢
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2017-12-02
相关资源
最近更新 更多