簡體   English   中英

Kotlin和Firebase雲函數update()方法

[英]Kotlin and Firebase cloud functions update() method

我正在使用Kotlin為Firebase雲功能生成Javascript代碼。

我想調用update方法,將一些值作為參數傳遞。 在Kotlin中,我必須傳遞一個Map作為update()的參數:

val map = mutableMapOf<String,Int>() //also tried with a Hashmap: same result

//generate some values
for(i in 0 until 3){ 
    map.put("key$i", i)
}
console.log(map.keys.toList().toString()) //log map keys
console.log(map.values.toList().toString()) //log map values

ref.child("values").update(map)

Kotlin生成javascript代碼:

var LinkedHashMap_init = Kotlin.kotlin.collections.LinkedHashMap_init_q3lmfv$;
function main$lambda(event){
    var map = LinkedHashMap_init();
    for (var i = 0; i < 3; i++) {
      map.put_xwzc9p$('key' + i, i);
    }
    console.log(toList(map.keys).toString());
    console.log(toList(map.values).toString());
    ref.child('values').update(map);
}

在我的Firebase控制台中,這是2個日志的結果。 它似乎表明地圖是正確的:

[key0, key1, key2]
[0, 1, 2]

但是update()不起作用:函數日志顯示該消息:

Error: Reference.update failed: First argument  contains an invalid key (this$AbstractMutableMap) in property 'games.test.values._keys_qe2m0n$_0'.  Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"
    at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/validation.js:139:27
    at Object.exports.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/cjs/src/obj.js:37:13)
    at Object.exports.validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/validation.js:132:16)
    at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/validation.js:223:17
    at Object.exports.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/cjs/src/obj.js:37:13)
    at Object.exports.validateFirebaseMergeDataArg (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/validation.js:221:12)
    at Reference.update (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/api/Reference.js:140:22)
    at main$lambda (/user_code/index.js:87:25)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
    at next (native)

但最后,如果我手動將js代碼寫入函數,它的工作原理如下:

var map = {}
for (var i = 0; i < 3; i++) {
    map["key"+i] = i
}
ref.child("values").update(map)

問候。

Kotlin顯然是在調用mutableMapOf創建了LinkedHashMap

update()接受一個JavaScript對象,其普通的簡單屬性應該用於更新文檔。

我很確定在JavaScript中實現LinkedHashMap有很多對象屬性會違反規定的約束,正如您可以從錯誤消息中看到的那樣。

您不能簡單地傳遞任何要update()的對象update() 您需要傳遞一個僅包含要update()的鍵和值的對象update() 該方法根本不期望像LinkedHashMap這樣復雜的東西。 相反,您需要編寫可編譯為簡單JavaScript對象的內容。

也許這會有助於閱讀

感謝Doug的回答 ,我寫了一個小片段來將一個簡單的鍵/值javascript對象包裝到Kotlin類型的對象:

class JsWrapperMap<K, V> {
    private val map: dynamic = js("({})")

    operator fun get(key: K): V = map[key]
    operator fun set(key: K, value: V) {
        map[key] = value
    }

    fun toJs(): dynamic = map
}

/**
 * Returns an empty new [JsWrapperMap]
 */
fun <K, V> jsMapOf(): JsWrapperMap<K, V> = JsWrapperMap()

/**
 * Returns an empty new [JsWrapperMap] of type <Any, Any>
 */
fun jsMapOfAny(): JsWrapperMap<Any, Any> = JsWrapperMap()

它可以像這樣使用:

val map = jsMapOf<String, Int>()
map["key_0"] = 12
map["key_1"] = 36
ref.child("values").update(map.toJs()) 

我對該代碼的任何建議持開放態度:)

暫無
暫無

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

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