Firebase函数(kotlin2js)错误:无法读取未定义的属性'iterator'

时间:2018-02-22 22:52:22

标签: javascript kotlin undefined google-cloud-functions

我正在编写一个Android应用程序,用于将数据存储在Firebase Firestore中,并使用Firebase云功能对其进行操作。我用kotlin2js编写了服务器代码。尝试迭代简单对象的属性时,Cloud Function会抛出一个未定义的错误。

为什么我收到此错误?

如何处理存储在对象中的数据?

1)MainActivity.kt

#include <stdio.h>

void testLocal()
{
  char s[] = "local"; // locally allocated 6 bytes initialized to "local\0"
  s[0] = 'L';
  printf("%s\n", s);
}

void testPointer()
{
  char *s = "pointer"; // locally allocated pointer, pointing to a constant
  // This segfaults on my system, but really the behavior is undefined.
  s[0] = 'P';
  printf("%s\n", s);
}

int main(int argc, char * argv[])
{
  testLocal();
  testPointer();
}

2)index.kt

    package com.srsly.wtf       

    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import com.google.firebase.firestore.FirebaseFirestore
    import kotlinx.android.synthetic.main.activity_main.button        

    class MainActivity : AppCompatActivity() {        

        override fun onCreate(bundle: Bundle?) {
            super.onCreate(bundle)
            setContentView(R.layout.activity_main)        

            button.setOnClickListener { _ -> upload() }
        }        

        fun upload() {
            FirebaseFirestore.getInstance().collection("test")
            .add(TestClass())
            .addOnSuccessListener { ref ->        

                ref.addSnapshotListener { snapshot, _ -> 
                    button.text = snapshot.toObject(TestClass::class.java).foo 
                }
            }
        }        

        data class TestClass(val foo = "bar")        

    }

3)index.js

    import kotlin.js.Promise        

    external fun require(module: String): dynamic
    external val exports: dynamic        

    fun main(args: Array<String>) {        

        val functions = require("firebase-functions")
        val admin = require("firebase-admin")

        admin.initializeApp(functions.config().firebase)        

        exports.receiveObj = functions.firestore.document("/test/{testId}").onWrite { event ->        

            val obj = event.data.data().unsafeCast<Map<String, String>>()        

            obj.entries.forEach{        

                val newObj = js("({})")
                newObj[it.key.toUpperCase()] = it.value.toUpperCase()        

                event.data.ref.set(newObj)
            }        

        }
    }

4)函数项目的build.gradle

    (function (_, Kotlin) {
      'use strict';

      var Unit = Kotlin.kotlin.Unit;

      function main$lambda(event) {
        var obj = event.data.data();
        var tmp$;

        tmp$ = obj.entries.iterator();

        while (tmp$.hasNext()) {

          var element = tmp$.next();
          var newObj = {};

          newObj[element.key.toUpperCase()] = element.value.toUpperCase();

          event.data.ref.set(newObj);
        }

        return Unit;
      }

      function main(args) {

        var functions = require('firebase-functions');
        var admin = require('firebase-admin');

        admin.initializeApp(functions.config().firebase);

        exports.receiveObj = functions.firestore.document('/test/{testId}').onWrite(main$lambda);
      }

      _.main_kand9s$ = main;

      main([]);
      Kotlin.defineModule('index', _);

      return _;

    }(module.exports, require('kotlin')));

5)来自firebase功能日志的错误消息

    buildscript {
        ext.kotlin_version = '1.2.21'        

        repositories {
            mavenCentral()
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }        

    version '1.0-SNAPSHOT'        

    apply plugin: 'java'
    apply plugin: 'kotlin2js'        

    sourceCompatibility = 1.8        

    repositories {
        mavenCentral()
    }        

    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
        testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }        

    compileKotlin2Js.kotlinOptions {
        moduleKind = "commonjs"
        outputFile = "functions/index.js"
    }

0 个答案:

没有答案