簡體   English   中英

更新字典中的“ /”在firebase中引發錯誤

[英]'/' in update dictionary throws error in firebase

我正在使用http端點上觸發的雲功能在firebase上進行數據庫遷移。 我基本上是遍歷數據庫並組裝一個更新字典,然后調用ref()。update(dict)。 我之前已經做過,但沒有任何問題,但是由於新的數據庫結構,我需要做類似的事情

teamUpdates['/calendar/' + visibleMonths[month] + '/' + matchId] = minifiedEvent

問題在於,由於某種原因,firebase不會將“ /”解析為路徑,因此給我一個錯誤:

第一個參數包含無效的密鑰(/ calendar / February 2018 / -L5n0GL4OjV3fnRg2wYV)。 鍵必須是非空字符串,並且不能包含“。”,“#”,“ $”,“ /”,“ [”或“]”。

在文檔中,它使用'/'字符定義路徑,而我之前已經成功使用過它,所以我不知道問題出在哪里。 有任何想法嗎?

這是他們在文檔中所做的:

var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;

我認為正在發生的事情是,我有一個函數,該函數返回帶有一些更新的對象,然后將其添加到第二個更新對象中(為了使我的函數更整潔,我在另一個函數中構建了該對象並返回了它)。 像這樣:

// This function returns the 'teamUpdates' object
const team = teamMigration(data[teamId])
// This is the dictionary that database.ref().update() gets called with
updates['/teams/' + teamId] = team

更新1:我重構了代碼以廢棄整個'teamUpdates'字典,並將所有內容直接嵌套在update ['/ teams /'+ teamId]下。

看起來很丑,但是行得通! 問題似乎在於,firebase不會將經過第一級的'/'解析為路徑,而是將其視為鍵,這顯然會失敗。

在我回答問題之前,我將等着看Firebase團隊成員是否對此進行了驗證。 感謝大家的幫助!

我只是在本地節點環境中運行了這段代碼:

var admin = require('firebase-admin');
var serviceAccount = require("./stackoverflow-3d9889aaeddb.json");
admin.initializeApp({ credential: admin.credential.cert(serviceAccount),databaseURL: "https://stackoverflow.firebaseio.com"});

var db = admin.database();
var ref = db.ref("50281844");
var updates = {};
updates['posts/newPostKey'] = "newKeyValue"
updates['user-posts/uid/newPostKey'] = "newKeyValue"
ref.update(updates)

並將其寫入數據庫:

{
  "posts" : {
    "newPostKey" : "newKeyValue"
  },
  "user-posts" : {
    "uid" : {
      "newPostKey" : "newKeyValue"
    }
  }
}

鍵中的多個斜杠在這里似乎可以正常工作,所以我要確定為什么它們對您不起作用。

參見https://stackoverflow.firebaseio.com/50281844.json

這段代碼是@Frank示例的修改,以演示@ kylar13使用的更新對象構造樣式。 這將失敗,並顯示“無效密鑰”錯誤:

  var db = admin.database();
  var ref = db.ref("50281844");
  var updates = {};
  updates['posts/newPostKey'] = "newKeyValue";
  // create a child map with '/' in key      
  var subUpdate = {};
  subUpdate['uid/newPostKey'] = "newKeyValue";
  // add it to the update map
  updates['user-posts'] = subUpdate;

  ref.update(updates)

@ kylar13:正如您在帖子更新中指出的那樣,對象鍵中的/ s只能位於根級對象中,而不能位於子對象中。 也許弗蘭克會看看這個並確認。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM