Typescript 如何从我的应用程序中正确调用HTTPs可调用函数?

Typescript 如何从我的应用程序中正确调用HTTPs可调用函数?,typescript,firebase,kotlin,google-cloud-functions,Typescript,Firebase,Kotlin,Google Cloud Functions,我按照一些指南编写了一个云函数。现在我需要从我的应用程序中调用它。我做了以下工作: Kotlin代码: class ActivitySignup : AppCompatActivity() { private lateinit var functions: FirebaseFunctions private lateinit var user: String override fun onCreate(savedInstanceState: Bundle?) {

我按照一些指南编写了一个云函数。现在我需要从我的应用程序中调用它。我做了以下工作:

Kotlin代码:

class ActivitySignup : AppCompatActivity() {

    private lateinit var functions: FirebaseFunctions
    private lateinit var user: String

    override fun onCreate(savedInstanceState: Bundle?) {
    ...
    functions = Firebase.functions
    ...
    submitbutton.setOnClickListener() {
            Log.e(tag,"Clicked submit")
            userEditTxt = findViewById(R.id.et_user)
            user = userEditTxt.text.toString().trim()

         auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.i(tag, "User created")

                    functions.getHttpsCallable("addUser")
                    .call(user)
                    .continueWith { task ->
                        // This continuation runs on either success or failure, but if the task
                        // has failed then result will throw an Exception which will be
                        // propagated down.
                        val result = task.result?.data as String
                        Log.e("result", result)
                        result
                    }

                    val intent = Intent(this, ActivityGroups::class.java)
                    startActivity(intent)
                    finish()

                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(tag, "failure", task.exception)
                    Toast.makeText(baseContext, "Authentication failed.",
                        Toast.LENGTH_SHORT).show()
                }

        }

    }
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

exports.addUser = functions.https.onCall((data, context) => {
  console.log('addUser: ', data.username);

  const username = data.username
  functions.auth.user().onCreate(user => {
    const doc = admin.firestore().collection('users').doc();
    return doc.set({
      createDate: admin.firestore.FieldValue.serverTimestamp(),
      modifiedDate: admin.firestore.FieldValue.serverTimestamp(), 
      username: username,
      email: user.email,    
      stat: 1, //0 = banned, 1 = normal
      uid: user.uid,
      rowpointer: doc.id,
    });
  });
index.ts中的云函数:

class ActivitySignup : AppCompatActivity() {

    private lateinit var functions: FirebaseFunctions
    private lateinit var user: String

    override fun onCreate(savedInstanceState: Bundle?) {
    ...
    functions = Firebase.functions
    ...
    submitbutton.setOnClickListener() {
            Log.e(tag,"Clicked submit")
            userEditTxt = findViewById(R.id.et_user)
            user = userEditTxt.text.toString().trim()

         auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.i(tag, "User created")

                    functions.getHttpsCallable("addUser")
                    .call(user)
                    .continueWith { task ->
                        // This continuation runs on either success or failure, but if the task
                        // has failed then result will throw an Exception which will be
                        // propagated down.
                        val result = task.result?.data as String
                        Log.e("result", result)
                        result
                    }

                    val intent = Intent(this, ActivityGroups::class.java)
                    startActivity(intent)
                    finish()

                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(tag, "failure", task.exception)
                    Toast.makeText(baseContext, "Authentication failed.",
                        Toast.LENGTH_SHORT).show()
                }

        }

    }
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

exports.addUser = functions.https.onCall((data, context) => {
  console.log('addUser: ', data.username);

  const username = data.username
  functions.auth.user().onCreate(user => {
    const doc = admin.firestore().collection('users').doc();
    return doc.set({
      createDate: admin.firestore.FieldValue.serverTimestamp(),
      modifiedDate: admin.firestore.FieldValue.serverTimestamp(), 
      username: username,
      email: user.email,    
      stat: 1, //0 = banned, 1 = normal
      uid: user.uid,
      rowpointer: doc.id,
    });
  });
然而,有两个问题:

  • Android studio以红色突出显示Firebase.functions的“函数”部分。错误是
    未解析的引用:函数

  • 当我在Visual Studio中执行
    firebase服务时,我得到了以下信息:

    • 函数[addUser]:http函数已初始化(http://localhost:5000/APPNAME-cf4da/us-central1/addUser)
    i功能:开始执行“addUser”

    {“严重性”:“警告”,“消息”:“请求的方法无效。获取”}

    {“严重性”:“错误”,“消息”:“请求无效,无法处理。”}

    i功能:在~1s内完成“添加用户”


    我对Android开发/云功能相当陌生,所以我觉得我只是在某个地方犯了一个新手错误…

    这种嵌套毫无意义:

    exports.addUser = functions.https.onCall((data, context) => {
      console.log('addUser: ', data.username);
    
      const username = data.username
      functions.auth.user().onCreate(user => {
        const doc = admin.firestore().collection('users').doc();
        return doc.set({
          createDate: admin.firestore.FieldValue.serverTimestamp(),
          modifiedDate: admin.firestore.FieldValue.serverTimestamp(), 
          username: username,
          email: user.email,    
          stat: 1, //0 = banned, 1 = normal
          uid: user.uid,
          rowpointer: doc.id,
        });
      });
    
    您似乎试图在
    functions.https.onCall
    函数中注册
    functions.auth.user().onCreate
    函数,这是不可能的。所有云函数都需要是
    index.js
    文件的顶级导出

    我的最佳猜测是,您希望将有关刚创建的用户的信息从Android代码传递到云函数,在这种情况下,应该在传递到
    onCall(数据、上下文)
    datsa
    参数中。如果您实际上“只是”想了解当前用户,也可以从
    context.auth
    中获取,如上的文档所示

    这可能更接近,尽管您的原始代码中可能存在更多问题:

    exports.addUser=functions.https.onCall((数据,上下文)=>{
    console.log('addUser:',data.username);
    const username=data.username;
    const email=data.email;
    
    const uid=context.auth.uid;//在您共享的Android代码中,我没有看到对可调用函数的任何调用。调用与此处文档中的示例类似:您确定这就是重现错误所需的全部吗?@frankvanpoffelen对此表示抱歉!我在Kotlin代码中添加了可调用函数的位置。我在pa中已经被告知我在我的问题中放了太多的代码…所以我试着在我的问题中只放重要的东西。还有什么是Kotlin应用程序的标准吗?@FrankvanPuffelen你链接的文档正是我为了达到这一点而遵循的。正是这部分给了我问题:感谢Android代码调用了这个函数。什么t是
    .call(用户)
    ?修复了!很抱歉。这是一个指向
    context.auth.uid
    的错误。错误是
    对象可能是“未定义的”
    。这可能是真的,因为我主要集中在尝试向您展示操作的概要。将此视为伪代码,向您展示代码不工作的一些关键点。我推荐您搜索有关错误消息的更多信息,该消息不是Firebase特有的。好的,谢谢!!!!如果我的答案有用,请单击“向上投票”按钮(▲) 如果它回答了您的问题,请单击复选标记(✓) 接受它。这样别人就会知道你已经得到了(足够的)帮助。也请参见