【问题标题】:Firebase database: how can I change value of list item?Firebase 数据库:如何更改列表项的值?
【发布时间】:2019-05-06 14:09:53
【问题描述】:

我的数据库中有这样的结构:

User 1
  +- items
       +- 1
       |  |- question: some question
       |  |- answer: some answer
       |
       +- 2
          |- question: another question

我想为第二个项目添加答案,但我没有 id。 我正在尝试获取物品并返回

[null, {answer: some answer, question: some question}, {question: another question}]

但如果没有 answer 字段,我将无法获得所需的项目,并且我无法获取他们的 ID。

我尝试使用equalTo,但没有帮助。

// attempt 1
Query child = _databaseReference.child(item._user).child(db_items).equalTo(null, key: db_answer);
child.reference().once().then((DataSnapshot snapshot) {
  print('snapshot: ${snapshot.value}');
});

// attempt 2
Query child = _databaseReference.child(item._user).child(db_items).equalTo(null, key: db_answer);
child.reference().child(db_question).once().then((DataSnapshot snapshot) {
  print('snapshot: ${snapshot.value}');
});

第一次尝试返回与我上面写的相同的输出,第二次返回 null

那么,有人知道如何将answer 字段添加到第二个问题吗?

附:我使用 Flutter,但我认为这对这个问题意义不大。

【问题讨论】:

    标签: firebase firebase-realtime-database flutter


    【解决方案1】:

    所以基本上你正在尝试将“答案”添加到你的第二个项目。 我会这样做: 首先是事先知道key,这样我就可以在第二项中插入一个子节点。这样我就可以写出类似的东西: myDBReference.child("items").child("key_known_beforehand").child("answer").setValue("this is the ansewr");

    或者如果我不知道密钥,这似乎是这里的问题: 我会使用 Firebase 查询。就像是.. myDBReference.child("items").orderByValue().startAt(starting_point_of_child_item).endAt(ending_point_of_child_item).child 这个查询会给我所需的节点,我会在那里设置值。

    "

    Using startAt(), endAt(), and equalTo() allows you to choose arbitrary starting and ending points for your queries
    

    要过滤数据,您可以在构造查询时将任何限制或范围方法与 order-by 方法结合使用。

    Unlike the order-by methods, you can combine multiple limit or range functions. For example, you can combine the startAt() and endAt() methods to limit the results to a specified range of values.
    

    "

    【讨论】:

    • 正如我所写 - 我没有钥匙,所以第一种方法不适合我的情况(或者我需要一些方法来获取这个钥匙)。关于第二种方式 - 你能解释一下如何在我的情况下使用它吗?在这些方法中必须发送哪些参数?
    • 我不在这里解释,而是给一个我使用的非常好的教程的链接。 howtofirebase.com/collection-queries-with-firebase-b95a0193745d希望对您有所帮助。祝你好运!
    • 实际上,我没有找到有关正确使用orderByValuestartAtendAt 的所需信息。如果您添加一些解释会很好
    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 2020-07-08
    • 2017-03-28
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多