Reputation: 4045
I have a node
project created with Vue
using Firebase Hosting
, Cloud Functions
and Firestore
.
I can deploy locally with npm run build | firebase serve
. In the Vue
app I'm calling a Cloud Function with firebase.functions().httpsCallable('placeOrder').then((result) => {...})
A
Is it possible to make it so that the Function
call calls the Functions emulator
(running on port 5001
) and I can step through the code with breakpoints?
B
If it's not possible to call the emulator from the locally hosted site, how can I do Cloud Functions
development locally? I've read a bunch of articles, but they seem to be outdated. I can emulate a function with the Functions Framework and get the logs when I call it, but I'm not sure how to attach a debugger to it. Also, what happens if that Function
reads / writes Firestore
? Will it read / write the emulated one?
Cheers
Upvotes: 1
Views: 284
Reputation: 1309
I'll tell you what had worked for me. I love firebase but, using the local emulators has been kind of like a pain for me. I know it will get better with time but is not there yet. Maybe someone else can give us a better approach
Use express and serve your function in the port 5001. You can attach the debugger to use breakpoints. Check here.
Folder structure
| functions/
----| index.js
----| app.js
----| tests/
--------| app.spec.js
| serve.js
index.js
const functions = require('firebase-functions');
const app = require('./app');
module.exports.app = functions.https.onRequest(app);
app.js
const express = require('express');
const app = express();
app.use('/', (req, res) => {
// do stuff
});
module.exports = app;
serve.js
const bodyParser = require('body-parser');
const port = 5001;
const app = require('./functions/app.js');
app.use(bodyParser.json());
const message = `app is listening on port ${port}!`;
app.listen(port, () => console.log(message));
Unit testing will make your life easier when dealing with cloud functions. They will make you save time and they will make you a better programmer at the same time.
Note: in this case, you are actually using the firestore database, so make sure you have a development environment. If you ever use the emulator for firestore it's in your local machine so it doesn't write to the real database. Hope that helps
Upvotes: 1