请求外部网络资源!- 来自 firebase 模拟器的日志错误

Yuv*_*n M 4 node.js express firebase google-cloud-functions google-cloud-firestore

我尝试注册并登录 firebase 。我使用Firebase (fire-store)、postman、express(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); 
Run Code Online (Sandbox Code Playgroud)

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"
      }
    ]
  }
}

Run Code Online (Sandbox Code Playgroud)

我正在使用 firebase emulators :start 启动 firebase emulator:start 启动 firebase emulator 时没有收到任何错误并且它工作正常但是,有一个警告,例如

!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
Run Code Online (Sandbox Code Playgroud)

如果我使用 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.
Run Code Online (Sandbox Code Playgroud)

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

m4c*_*eth 5

我刚刚遇到了同样的问题,这似乎归结为admin.initializeApp();需要与您可能提供的信息不同的信息。

我在这里找到了这个有帮助的答案

我猜您从控制台 > 项目设置 > 常规 > 您的应用 > Firebase SDK 代码段获取了配置 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"
});
Run Code Online (Sandbox Code Playgroud)

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

  • 不适合我。仍然出现错误。:-( (2认同)