【问题标题】:Firebase add 'g.Bg.Cg' to object keyFirebase 将“g.Bg.Cg”添加到对象键
【发布时间】:2018-05-24 00:25:04
【问题描述】:

我想获取频道的密钥并将此密钥添加到路径中。这是我的代码:

firebase.database().ref('channels/').orderByKey().limitToLast(1).once('value').then(function (snapshot) {
            const channel =  Object.keys(snapshot.val())[0];
            createUserChannel(channel, currentUser, db);
....
function createUserChannel(channel, currentUser, db) {
            db.list('channelUsers/' + channel).push(currentUser);
        }

但我有这个错误:

ERROR Error: Uncaught (in promise): Error: Reference.push failed: 
first argument contains a function in property 'channelUsers.- 
LDCclTrWKekW0MqUy-n.g.Bg.Cg' with contents = function XMLHttpRequest() { 
[native code] }

我的频道密钥中没有“g.Bg.Cg”。这是什么?

【问题讨论】:

  • 很难说到底发生了什么,因为我们看不到channeldb,currentUser 的值。您能否使用每个硬编码值重现问题,然后更新您的问题以显示这些值?

标签: javascript angular firebase firebase-realtime-database


【解决方案1】:

你应该这样做:

    firebase.database().ref('channels/').orderByKey().limitToLast(1).once('value').then(function (snapshot) {
       snapshot.forEach(function(childSnapshot) {
         const channel = childSnapshot.key;
        // ...
       });

    //..............

查看文档:https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#key

here,查询的文档,其中说“即使查询只有一个匹配项,快照仍然是一个列表;它只包含一个项目。要访问该项目,您需要循环遍历结果:"

【讨论】:

  • 对不起,这是我的错误。由于您进行查询,因此我们需要遍历子代。我已经更新了。
  • 再次出现同样的错误。当我 console.log 频道时,我有右键,但是当我推送时,我得到了同样的错误:(
  • 你应该使用set或者update,具体看push这里firebase.google.com/docs/reference/js/…
  • 是的,另一个问题是在 push() 中。现在它可以正常工作了,谢谢!
  • 很高兴能帮到你。您可以通过单击答案左侧的灰色复选标记来接受答案,请参阅stackoverflow.com/help/accepted-answer。感谢并欢迎来到 SO。
猜你喜欢
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2022-06-29
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 2019-12-23
相关资源
最近更新 更多