【问题标题】:firebase transaction dynamic keyfirebase 交易动态密钥
【发布时间】:2017-05-10 11:49:42
【问题描述】:

我有以下 JSON,我正在尝试在已登录用户的帐户上添加一个事务以“计数”,但是 firebase 广告动态密钥,我在这方面遇到了 firebase 引用问题。

{
  "accounts" : {
    "-KRPSyO4B48IpBnHBTYg" : {
      "count" : "100",
      "email" : "",
      "userId" : ""
    }
  },
  "products" : {
    "-KUKRafaNurpFhGF4jQa" : {
      "name" : ""
    }
  },
}

我正在努力做到这一点:

firebase.database().ref('accounts/' + accounts.id+'/count').transaction(function(value) {
  if (value) {
    value++;
  }
 return value;
});

这一切都很好,除了我不知道“accounts.id”是什么,即使用户登录。

更新已登录用户的计数记录的正确方法是什么?

【问题讨论】:

  • /accounts 下的数据是什么意思?如果是用户列表,则应将它们存储在用户的 UID 下,而不是推送 ID。
  • 我明白了,userId 也有一个动态键,绝对是更好的方法。该数据确实是用户帐户。
  • 好的。在这种情况下:将用户存储在他们的 UID 下。如果需要使用推送 ID,则需要编写查询来确定包含所需 UID 的推送 ID。

标签: javascript firebase firebase-realtime-database


【解决方案1】:

您可以通过以下方式获取登录用户的详细信息:

var user = firebase.auth().currentUser;
if (user) {
    userId = user.uid
} else {
    // No user is signed in.
}

一旦你有了用户 ID,你的代码就应该可以工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 2017-02-28
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多