【问题标题】:External network resource requested! - log error from firebase emulator请求外部网络资源! - 来自 firebase 模拟器的日志错误
【发布时间】:2020-09-24 07:56:24
【问题描述】:

我尝试注册并登录 firebase 。我使用 Firebase(火库), 邮递员,快递(REST API)

我的代码(index.js)

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp(config); 
// i'm not  provide the config data here, but initialized in my actual code.

const express = require("express");
const app = express();

let db = admin.firestore();

// Signup route 
app.post('/signup', (req,res) => {
  const newUser = {
    email: req.body.email,
    password: req.body.password,
    confirmPassward: req.body.confirmPassword,
    handle: req.body.handle
  };

  let token, userId;
  db.doc(`/users/${newUser.handle}`)
    .get()
      .then(doc => {
        if(doc.exists) {
          return res.status(400).json({ hanldle: 'this hanlde is already taken'});
        }else {
          return firebase()
        .auth()
        .createUserWithEmailAndPassword(newUser.email, newUser.password);
        }
      })

    .then((data) => {
       userId = data.user.uid;
      return data.usergetIdToken()
    })
.then( ( idToken ) => {
      token = idToken ;
      const userCredentials = {
        handle: newUser.handle,
        email: newUser.email,
        createdAt: new Date().toISOString(),
        userId 
      };
      return db.doc(`/users/${newUser.handle}`).set(userCredentials);
    })
    .then(() => {
      return res.status(201).json({ token });
    })
    .catch(err => {
      if(err.code === 'auth/email=already-in-use') {
        return res.status(400).json({ email: 'email is alread is used '})
      } else { 
        return res.status(500).json({ err : err.code });
      }
    });
});

exports.api = functions.https.onRequest(app); 

firebase.json 文件

{
  "emulators": {
    "functions": {
      "port": 5001
    },
    "ui": {
      "enabled": true
    },
    "hosting": {
      "port": 5000
    }
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

我正在使用 firebase emulators:start 启动 firebase 模拟器 启动 firebase 模拟器时我没有收到任何错误,它工作正常虽然,有一个警告,比如

!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.

如果我使用 post 发送任何 get 或 post 请求,我会从模拟器中获取错误日志

!  External network resource requested!
   - URL: "http://169.254.169.254/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
!  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
>  Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.

我不知道如何摆脱它。如果您能在这件事上给我任何帮助,我们将不胜感激。

【问题讨论】:

    标签: node.js firebase express google-cloud-firestore google-cloud-functions


    【解决方案1】:

    我刚刚遇到了同样的问题,看来admin.initializeApp(); 需要的信息与您可能提供的信息不同。

    我在这里找到了this answer,这很有帮助。

    我猜你从控制台 > 项目设置 > 常规 > 你的应用 > Firebase SDK sn-p 获得了配置 JSON。这就是我正在使用的,并且也不断收到此错误。

    然后我按照那个答案的建议做了。转到控制台 > 项目设置 > 服务帐户。在那里你会找到一个蓝色按钮,上面写着“生成新的私钥”。下载该密钥,将其保存到您的项目中(如果您愿意,可以重命名)。然后按照该页面的建议,使用以下代码进行初始化。

    var admin = require("firebase-admin");
    
    var serviceAccount = require("path/to/serviceAccountKey.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://projectname.firebaseio.com"
    });
    

    值得注意的是,databaseURL 位可以在您当前拥有的 config 中找到。

    【讨论】:

    • 对我不起作用。仍然收到错误。 :-(
    【解决方案2】:

    首先,常规 Firebase 帐户存在限制。

    一旦您进入 blaze 计划等,请确保“项目设置”>“服务帐户”选项卡 > Firebase Admin SDK

    生成新的服务密钥。

    然后使用下面的代码映射正确的服务帐户 json

    var admin = require("firebase-admin");
    
     var serviceAccount = require("Mention path to service Account key.json")
    
     admin.initializeApp({
       credential: admin.credential.cert(serviceAccount)
    })
    

    谢谢你 m4cbeth。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多