Firebase投放错误时,Firebase部署工作正常

时间:2020-07-25 07:48:58

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

我正在关注a tutorial

我对firebase的理解是firebase deploy需要花费一些时间来部署和传播。因此,开发人员倾向于使用firebase serve来获得热重载功能。

我尝试使用firebase deployfirebase serve运行此简单代码。 firebase deploy正常工作,我可以使用Postman发送GET请求,而firebase serve发送错误。

如何重现环境

  1. npm install -g firebase-tools

  2. firebase初始化,选择功能,选择javascript

  3. 将下面的代码添加到index.js

  4. firebase服务

  5. 使用Postman发送GET请求到api/screams

index.js

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

admin.initializeApp();

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

app.get('/screams', (req, res) => {
    admin
        .firestore()
        .collection('screams')
        .get()
        .then((data) => {
            let screams = [];
            data.forEach((doc) => {
                screams.push(doc.data());
            });
            return res.json(screams);
        })
        .catch((err) => console.error(err));
});

app.post('/scream', (req, res) => {
    const newScream = {
        body: req.body.body,
        userHandle: req.body.userHandle,
        createdAt: admin.firestore.Timestamp.fromDate(new Date())
    };

    admin
        .firestore()
        .collection('screams')
        .add(newScream)
        .then((doc) => {
            res.json({ message: `document ${doc.id} created successfully` });
        })
        .catch((err) => {
            res.status(500).json({ error: `something went wrong` });
            console.error(err);
        });
});

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

错误

=== Serving from 'C:\Users\jason\Documents\GitHub\functions'...

!  Your requested "node" version "10" doesn't match your global version "12"
i  functions: Watching "C:\Users\jason\Documents\GitHub\functions\functions" for Cloud Functions...
+  functions[api]: http function initialized (http://localhost:5000/functions/us-central1/api).
i  functions: Beginning execution of "api"
!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
!  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.
>      at GoogleAuth.getApplicationDefaultAsync (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:160:19)
>      at processTicksAndRejections (internal/process/task_queues.js:97:5)
>      at async GoogleAuth.getClient (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:502:17)
>      at async GrpcClient._getCredentials (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-gax\build\src\grpc.js:92:24)
>      at async GrpcClient.createStub (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-gax\build\src\grpc.js:213:23)
>  Caused by: Error
>      at CollectionReference._get (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\@google-cloud\firestore\build\src\reference.js:1485:23)
>      at CollectionReference.get (C:\Users\jason\Documents\GitHub\functions\functions

编辑:当前解决方案是直接使用firebase emulators:start而不是firebase serve

edit2:根据Doug Stevenson的建议添加package.json。

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

0 个答案:

没有答案