“确定执行环境的意外错误";运行Firebase模拟器时 [英] "Unexpected error determining execution environment" when running Firebase emulator

查看:63
本文介绍了“确定执行环境的意外错误";运行Firebase模拟器时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我部署函数时,一切正常,但是使用Firebase模拟器在本地运行相同的代码会给我以下错误:

Everything works correctly when I deploy my functions, but running the same code locally with the Firebase emulator gives me the following error:

{
  "message": "Unexpected error determining execution environment: request to http://169.254.169.254/computeMetadata/v1/instance failed, reason: connect EHOSTDOWN 169.254.169.254:80 - Local (192.168.1.101:56456)",
  "type": "system",
  "errno": "EHOSTDOWN",
  "code": "EHOSTDOWN",
  "config": {
    "url": "http://169.254.169.254/computeMetadata/v1/instance",
    "headers": {
      "Metadata-Flavor": "Google"
    },
    "retryConfig": {
      "noResponseRetries": 0,
      "currentRetryAttempt": 0,
      "retry": 3,
      "retryDelay": 100,
      "httpMethodsToRetry": [
        "GET",
        "HEAD",
        "PUT",
        "OPTIONS",
        "DELETE"
      ],
      "statusCodesToRetry": [
        [
          100,
          199
        ],
        [
          429,
          429
        ],
        [
          500,
          599
        ]
      ]
    },
    "responseType": "text",
    "timeout": 3000,
    "params": {},
    "method": "GET"
  }
}

我应该怎么做才能使用模拟器测试代码?

What should I do to be able to test my code with the emulator?

推荐答案

如果您的云功能需要管理员权限,则需要配置一个服务帐户,以使云功能可以在其下运行.

If your cloud function requires admin rights, then you need to configure a service account for the cloud functions to run under.

按照此处的说明进行操作: https://firebase.google.com/docs/admin/setup

Follow the instructions here: https://firebase.google.com/docs/admin/setup

1)创建一个服务帐户并下载.json私钥文件(保留此机密)

1) Create a Service Account and download the .json private key files (keep this secret)

在将要运行模拟器的控制台中:

2)将GOOGLE_APPLICATION_CREDENTIALS环境变量设置为步骤1中保存的私钥文件的绝对路径.例如,export GOOGLE_APPLICATION_CREDENTIALS="/Users/..../...account.json"

2) Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the absolute path of the private key file saved in step 1. For example export GOOGLE_APPLICATION_CREDENTIALS="/Users/..../...account.json"

3)现在运行模拟器firebase emulators:start --only functions

3) Now run your emulator firebase emulators:start --only functions

现在,本地仿真函数可以了解环境.因此,当您执行admin.initializeApp(functions.config().firebase);之类的操作时,其配置正确.

Now the locally emulated function knows the environment. So when you do something like admin.initializeApp(functions.config().firebase); it gets configured properly.

这篇关于“确定执行环境的意外错误";运行Firebase模拟器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆