Kotlin和Firebase云函数update()方法

Mal*_*kDe 2 javascript kotlin firebase google-cloud-functions

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

在我的Firebase控制台中,这是2个日志的结果.它似乎表明地图是正确的:

[key0, key1, key2]
[0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

但是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)
Run Code Online (Sandbox Code Playgroud)

但最后,如果我手动将js代码写入函数,它的工作原理如下:

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

问候.

Dou*_*son 5

Kotlin显然正在通过你的电话制作一个LinkedHashMapmutableMapOf.

update() 采用一个JavaScript对象,其普通的简单属性应该用于更新文档.

我很确定在JavaScript中实现LinkedHashMap有很多对象属性会违反规定的约束,正如您可以从错误消息中看到的那样.

你不能简单地传递任何你想要的对象update().您需要传递一个只包含您想要的键和值的对象update().该方法根本不是期望像a一样复杂的东西LinkedHashMap.相反,您需要编写可编译为简单JavaScript对象的内容.

也许这会有助于阅读.