Skip to content

Instantly share code, notes, and snippets.

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
console.log('Convert your Firestore collection to CSVs every day at 11:05 AM Eastern!');
return null;
});
import * as functions from "firebase-functions";
import * as firebase from "firebase-admin";
import { parseAsync } from 'json2csv';
import { v4 as uuidv4 } from 'uuid';
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
firebase.initializeApp({
storageBucket: 'storage-bucket-name',
i functions: Beginning execution of "calculateOrder"
i Your code does not appear to initialize the 'firebase-admin' module, so we've done it automatically.
- Learn more: https://firebase.google.com/docs/admin/setup
> begin trigger
⚠ Unknown network resource requested!
- URL: "http://metadata.google.internal./computeMetadata/v1/instance"
⚠ Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/Users/shangyilim/Projects/emulator-test/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)
at processTicksAndRejections (internal/process/task_queues.js:86:5)
⚠ Your function was killed because it raised an unhandled error.
Starting emulators: ["functions","firestore"]
⚠ Your requested "node" version "8" doesn't match your global version "11"
✔ functions: Emulator started at http://localhost:5001
i firestore: Logging to firestore-debug.log
✔ firestore: Emulator started at http://localhost:8080
i firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i functions: Watching "/Users/shangyilim/Projects/emulator-test/functions" for Cloud Functions...
i functions: Setting up Cloud Firestore trigger "calculateOrder"
✔ functions: Trigger "calculateOrder" has been acknowledged by the Cloud Firestore emulator.
Firestore.instance.settings(host: "10.0.2.2:8080", sslEnabled: false );
const functions = require('firebase-functions');
exports.calculateOrder = functions.firestore
.document("orders/{orderId}")
.onWrite(async (change, context) => {
console.log('begin trigger');
return change.after.ref.update({
orderUpdated: true
});
import { google } from 'googleapis';
app.post('/sendSMS', function (req, res) {
const { phoneNumber, recaptchaToken } = req.body;
const identityToolkit = google.identitytoolkit({
auth: 'GCP_API_KEY',
version: 'v3',
});
import { google } from 'googleapis';
app.post('/sendSMS', function (req, res) {
const { phoneNumber, recaptchaToken } = req.body;
const identityToolkit = google.identitytoolkit({
auth: 'GCP_API_KEY',
version: 'v3',
});
// set-up an invisible recaptcha. 'sendCode` is button element id
// <button id="sendCode">Send Code</button>
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sendCode', {
'size': 'invisible',
'callback': function (recapchaToken) {
// reCAPTCHA solved, send recapchaToken and phone number to backend.
// a REST call to your backend
request.post({
url: 'https://your-rest-api',
export const deletePhoto = functions.firestore
.document("posts/{postId}")
.onUpdate((change, context) => {
// Get an object representing the document
const updatedPost = change.after.data() as any;
// ...or the previous value before this update
const oldPost = change.before.data() as any;