首页 > 解决方案 > 无论输入或函数名称如何,httpsCallable 在使用 expo 运行模拟器并反应原生时都会返回错误:“internal”

问题描述

我们正在开发一个带有 expo 托管工作流和 firebase 的应用程序,但似乎无法在应用程序中使用 httpsCallable 触发模拟的云功能。我们总是收到“错误:内部”。该函数似乎没有被调用。从“addMessage2”更改函数名称不会更改错误。也不会改变输入。调用 httpsCallable 时产生错误。

知道什么是错的或我们应该改变什么吗?

错误:

internal
at node_modules\@firebase\auth\dist\auth.esm.js:420:103 in <anonymous>
at node_modules\@firebase\auth\dist\auth.esm.js:422:422 in <anonymous>
at node_modules\@firebase\functions\dist\index.esm.js:119:12 in _errorForResponse
at node_modules\tslib\tslib.js:84:8 in __rest
at node_modules\tslib\tslib.js:38:4 in <global>
at http://192.168.1.85:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:162043:31 in fulfilled
at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
at [native code]:null in flushedQueue
at [native code]:null in callFunctionReturnFlushedQueue

客户:

// Firebase App (the core Firebase SDK) is always required and must be listed first
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/functions'
import 'firebase/firestore'
import React from 'react'

// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: '...',
  authDomain: '...',
  databaseURL: '...',
  projectId: '...',
  storageBucket: '...',
  messagingSenderId: '...',
  appId: '...',
  measurementId: '...',
}

const app = firebase.initializeApp(firebaseConfig)
var functions = app.functions()

if (__DEV__) {
  console.log('Switching to local Firebase instance...')
  const origin = 'localhost'
  firebase.functions().useEmulator(origin, 5001)
}

函数调用(客户端):

  const addMessage = functions.httpsCallable('addMessage2')
  addMessage()
    .then(data => console.log('it worked', data))
    .catch(error => console.error(error))

服务器:

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

export const addMessage2 = functions.https.onCall((data: { text: string }, context) => {
  // Checking attribute.
  if (!data || !(typeof data.text === 'string') || data.text.length === 0) {
    // Throwing an HttpsError so that the client gets the error details.
    throw new functions.https.HttpsError(
      'invalid-argument',
      'The function must be called with ' +
        'one arguments "text" containing the message text to add.'
    )
  }


  // Checking that the user is authenticated.
  if (!context.auth) {
    // Throwing an HttpsError so that the client gets the error details.
    throw new functions.https.HttpsError(
      'failed-precondition',
      'The function must be called ' + 'while authenticated.'
    )
  }

  // returning result, must be able to json encode. If async, return a promise.
  return {
    text: 'hello',
  }
})

标签: firebaseexpo

解决方案


推荐阅读