I am following a tutorial.
My understading about firebase is firebase deploy
takes time to deploy and propagate. Therefore, developer tends to use firebase serve
to get a hot reload feature.
I tried to run this simple code with firebase deploy
and firebase serve
. The firebase deploy
works as expected and I can send a GET request using Postman
while firebase serve
sent an error.
How to reproduce the environment
npm install -g firebase-tools
firebase init, select functions, select javascript
Add the code down below to index.js
firebase serve
use Postman to send a GET request to
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);
error
=== 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
edit: current solution is to use firebase emulators:start
directly instead of firebase serve
edit2: add package.json as recommended by Doug Stevenson.
{
"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
}