Firebase Realtime Database Emulator: Can't read from database (snapshot is always null) even though onWrite function gets triggered - node.js

I have a very simple Cloud Function running in the emulator:
functions.region('europe-west1').database.ref('teams/{teamId}/members/{memberId}')
.onWrite(async (change, context) => {
const snapshot = await admin.database().ref('teams').once('value')
console.log(26, snapshot.exists())
console.log(snapshot.val())
})
When I change a member or add a new member in the emulator this function gets triggered (so 'teams' is 100% defined) however the snapshot is always empty and does not exist.
I am staring the emulator with the following command :
firebase emulators:start --import ./folder
The folder has a JSON file with my database.
Anyone has any idea what is causing this? So frustrating, literally always see null in the console.

I had two different database instances in different regions. The emulator was listening for changes in database A but reading in database B. I had only emulated database A.

You need to export the function in order to run it in the local Firebase emulator. You may also refer to these documentation on how Execution Environment works.
I have reproduced the error from your given code. Below is working sample function based on your given code:
var admin = require("firebase-admin");
var functions = require("firebase-functions")
 
// Initialize the app with a service account, granting admin privileges
admin.initializeApp();
 
// As an admin, the app has access to read and write all data, regardless of Security Rules
 
exports.simpleDbFunction = functions.region('europe-west1').database.ref('teams/{teamId}/members/{memberId}')
.onWrite(async (change, context) => {
const snapshot = await admin.database().ref('teams').once('value')
console.log(26, snapshot.exists())
console.log(snapshot.val())
})

Related

Firebase Function how to call a region real time database

I am struggling with one issue:
I have a cloud function that trigger on the realtime database
.ref("/games/...")
.onWrite(async (change) => {
It's working pretty well on the default realtime database (asia) but not on the europe-west1
So I tried functions.regions('europe-west1').database but no success, I couldn't find any help online.
Thanks for your help
The .region() specifies where the Cloud function will run and has nothing to with database. You are probably looking for instance():
instance() registers a function that triggers on events from a specific Firebase Realtime Database instance.
By default functions.database.ref used without instance watches the default instance for events and that's why the trigger works for your default DB in Asia.
firebase.database.instance('my-app-db-2').ref('/foo/bar').onWrite(async (change) => {})
Checkout 'specify the instance and path' section in the documentation.

Access Playfab data with Azure functions

How can I access to read/write/modify/delete Playfab data with Azure functions?
Below Steps will help you in accessing the azure functions with Playfab
Create Azure Function in VS Code
Deploy the azure function to portal and register your function with cloud script
Below is the sample example code for calling cloud script using azure functions from your playfab.
//This snippet assumes that your game client is already logged into PlayFab.
using PlayFab;
using PlayFab.CloudScriptModels;
private void CallCSharpExecuteFunction()
{
PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
{
Entity = new PlayFab.CloudScriptModels.EntityKey()
{
Id = PlayFabSettings.staticPlayer.EntityId, //Get this from when you logged in,
Type = PlayFabSettings.staticPlayer.EntityType, //Get this from when you logged in
},
FunctionName = "HelloWorld", //This should be the name of your Azure Function that you created.
FunctionParameter = new Dictionary<string, object>() { { "inputValue", "Test" } }, //This is the data that you would want to pass into your function.
GeneratePlayStreamEvent = false //Set this to true if you would like this call to show up in PlayStream
}, (ExecuteFunctionResult result) =>
{
if (result.FunctionResultTooLarge ?? false)
{
Debug.Log("This can happen if you exceed the limit that can be returned from an Azure Function, See PlayFab Limits Page for details.");
return;
}
Debug.Log($"The {result.FunctionName} function took {result.ExecutionTimeMilliseconds} to complete");
Debug.Log($"Result: {result.FunctionResult.ToString()}");
}, (PlayFabError error) =>
{
Debug.Log($"Opps Something went wrong: {error.GenerateErrorReport()}");
});
}
PlayFab CloudScript Context, Variables and Server SDKs
You will need to install the PlayFab SDK via Package Manager. To do this open Terminal or CMD Console in Visual Studio Code and type: dotnet add package PlayFabAllSDK
We have created some helpers that will ship with the cSharpSDK.
You need to edit your .csproj file and include <DefineConstants>NETCOREAPP2_0</DefineConstants> in your default PropertyGroup or NETCOREAPP3_1 if you are using the latest.
Execution of a script can occur through several methods (APIs, Scheduled Tasks, PlayStream Event, Segment Entering and Exit method). The context of the execution is important to implement your CloudScript. See the Using CloudScript context models tutorial for details on how to use the context of the script.
For further information check Cloud Script Using Azure Functions and Playfab Cloud Script

Google Cloud Platform / Firebase Function not triggering with onWrite

My application makes use of Firestore Function Triggers to perform background actions based on changes in the Firestore, e.g. user profile changes. For example, if they change their mobile number, a verification code is sent.
I have a trigger that should run when an onWrite() event happens on a specific collection. onWrite() runs when any of the following actions occur in Firebase on a specific collection:
onCreate()
onUpdate()
onDelete()
In my usecase, I need it to run for onCreate() and onUpdate(), thus I use onWrite()
For Firebase Triggers to work, a specific format is expected in addition to a document id/wildcard representing a document that was created/changed/deleted.
Constants:
const collections = {
...
conversations: "conversations",
...
}
Callable Function (updates firestore):
/**
* Add an conversation to conversations collection
* #type {HttpsFunction & Runnable<any>}
*/
exports.addConversations = functions.https.onCall(async (data, context) => {
// expects conversation & interested state
const {valid, errors} = validateRequiredBody(data, [
"conversation",
]);
if (!valid) {
return {
status: false,
message: "Missing or invalid parameters",
errors: errors,
jwtToken: "",
};
}
// get conversation item
const conversation = {
id: data["conversation"]["id"],
name: data["conversation"]["name"],
}
// create conversation with empty counter
// let writeResult = await collectionRefs.conversationsRef.doc(conversation.id).set({
let writeResult = await admin.firestore().collection(collections.conversations).doc(conversation.id).set({
id: conversation.id,
name: conversation.name,
count: 0
});
console.log(`[function-addConversations] New Conversation [${conversation.name}] added`);
return {
status: true,
message: ""
}
});
Firestore Trigger (not triggering):
/**
* On conversations updated/removed, update corresponding counter
* #type {CloudFunction<Change<QueryDocumentSnapshot>>}
*/
exports.onConversationProfileCollectionCreate = functions.firestore.document(`${collections.conversations}/{id}`)
.onWrite(async snapshot => {
console.log("Conversation Collection Changed");
// let conversation = collectionRefs.conversationsRef.doc(snapshot.id);
// await conversation.update({count: FieldValue.increment(1)});
});
In my mobile application, the user (calls) the addConversations() firebase function, this adds the new conversation to Firestore which is clearly visible, but the counter trigger (trigger function) doesn't run.
Emulator output:
...
{"verifications":{"app":"MISSING","auth":"MISSING"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-request-verification"},"severity":"INFO","message":"Callable request verification passed"}
[function-addConversations] New Conversation [Test Conversation Topic] added
Profile updated
(print profile data)
...
What I SHOULD expect to see:
...
{"verifications":{"app":"MISSING","auth":"MISSING"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-request-verification"},"severity":"INFO","message":"Callable request verification passed"}
[function-addConversations] New Conversation [Test Conversation Topic] added
Conversation Collection Changed // this is output by the trigger
Profile updated
(print profile data)
...
Did I do something wrong?
The issue was one closer to home.
I am developing using the firebase emulators and connecting to them using the emulators addition in Flutter's firebase packages built in emulator feature.
Firebase emulator setup e.g. Firebase Functions can be found here
TL;DR:
When starting your Firebase emulator, you should see something similar to:
C:\Users\User\MyApp\awesome-app-server>firebase emulators:start --only functions
i emulators: Starting emulators: functions
! functions: You are running the functions emulator in debug mode (port=9229). This means that functions will execute in sequence rather than in parallel.
! functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, hosting, pubsub, storage
! Your requested "node" version "12" doesn''t match your global version "14"
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "C:\Users\User\MyApp\awesome-app-server\functions" for Cloud Functions...
> Debugger listening on ws://localhost:9229/03dc1d62-f2a3-418e-a343-bb0b357f7329
> Debugger listening on ws://localhost:9229/03dc1d62-f2a3-418e-a343-bb0b357f7329
> For help, see: https://nodejs.org/en/docs/inspector
! functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
! functions: The Realtime Database emulator is not running, so calls to Realtime Database will affect production.
...
BUT then you see this - THIS is very important!
i functions[us-central1-api-user-onConversationProfileCollectionCreate ]: function ignored because the database emulator does not exist or is not running.
This, since Firestore & (Realtime) Database use triggers which are found in functions, functions expects to find a local firestore/database emulator.
Since no firestore/database emulators were running locally
! functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
! functions: The Realtime Database emulator is not running, so calls to Realtime Database will affect production.
and these functions don't automagically attach to production Firestore/Database(s) (that would be potentially devestating), these triggers didn't run when I expected them to while emulating locally.
Solution:
Emulate firestore & database locally (see this to import your firestore data to a local emulator) with firebase emulators:start --only functions,...,firestore,database
Upload functions to work with Firestore/Database(s) (please do this with care)
More details:
Below I provide details what lead me to the problem, and how I figured out the issue:
What I was doing:
firebase emulators:start --inspect-functions --only functions,auth
[local] Firebase Functions I was developing and testing the backend for my mobile app using Firebase Functions for various interactivity.
Since Firebase Auth is handled locally through the use of custom tokens on my firebase functions app, I used local auth for testing
I had prepopulated my Firestore with data, thus I intended to use Firestore data while emulating locally which had worked as expected, BUT the firebase firestore & database triggers won't work due to emulating them locally.
I knew some of my triggers DID infact trigger correctly, thus it must be a configuration error of some kind.
Extended Solution (import firestore data to local emulator)
firebase emulators:start --inspect-functions --only functions,auth,firestore,database
notice the last 2, firestore & database - this should be added for triggers to work locally
I noticed when reviewing the logs at startup, the text indicating some functions won't run. This made me realize I was making a crucial mistake - the cause.
Import Data from production firestore
The reason for using production (during development) firestore is to not recreate all the data for each test. The solution is to export from firestore and import you data to the emulators.
See this for details - I wasn't able to export from the terminal, I thus went to Google Console to export to a storage bucket, and download it via the console from there.
To start the emulator (and allow debugging of firebase functions), I use:
firebase emulators:start --inspect-functions --import ./functions/{path/to/data} --only functions,auth,firestore,database
details:
firebase emulators:start - start emulator
--inspect-functions allow debugging functions via websockets - e.g. Webstorm NodeJs debugger on port 9229
--import ./functions/{path/to/data} - import data to firestore emulator using project root as ./
--only functions,auth,firestore,database specify the modules to emulate
You are using the wrong value. snapshot works only for onDelete and onCreate method
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {
// If we set `/users/marie` to {name: "Marie"} then
context.params.userId == "marie"
// ... and ...
change.after.data() == {name: "Marie"}
});
for more info read here feedbackCloud Firestore triggers

Can I avoid using live Firebase Storage when using emulators?

as I patiently wait for Firebase storage to be added to the emulators, I was wondering if there is a way I can avoid modifying live storage files and folders when running hosting / functions in the emulator?
For example I use the following code to delete all the files in a folder. Last night someone accidentally deleted all the documents in our emulator as part of a test and it deleted all the LIVE storage folders as we use an import of real documents into our emulator 🤦
async function deleteStorageFolder(path:string) {
const bucket = admin.storage().bucket();
return bucket.deleteFiles({
prefix: path
})
Is there any way I can tell firebase to avoid using the production storage APIs when emulators are running?
I have used the following condition in my function to prevent using firebase storage API when running in emulator:
if (process.env.FUNCTIONS_EMULATOR == "true") {
console.log(`Running in emulator, won't call firebase storage`)
} else {
// Code goes here to run storage APIs
}

Trying to insert data into BigQuery fails from container engine pod

I have a simple node.js application that tries to insert some data into BigQuery. It uses the provided gcloud node.js library.
The BigQuery client is created like this, according to the documentation:
google.auth.getApplicationDefault(function(err, authClient) {
if (err) {
return cb(err);
}
let bq = BigQuery({
auth: authClient,
projectId: "my-project"
});
let dataset = bq.dataset("my-dataset");
let table = dataset.table("my-table");
});
With that I try to insert data into BiqQuery.
table.insert(someRows).then(...)
This fails, because the BigQuery client returns a 403 telling me that the authentication is missing the required scopes. The documentation tells me to use the following snippet:
if (authClient.createScopedRequired &&
authClient.createScopedRequired()) {
authClient = authClient.createScoped([
"https://www.googleapis.com/auth/bigquery",
"https://www.googleapis.com/auth/bigquery.insertdata",
"https://www.googleapis.com/auth/cloud-platform"
]);
}
This didn't work either, because the if statement never executes. I skipped the if and set the scopes every time, but the error remains.
What am I missing here? Why are the scopes always wrong regardless of the authClient configuration? Has anybody found a way to get this or a similar gcloud client library (like Datastore) working with the described authentication scheme on a Container Engine pod?
The only working solution I found so far is to create a json keyfile and provide that to the BigQuery client, but I'd rather create the credentials on the fly then having them next to the code.
Side note: The node service works flawless without providing the auth option to BigQuery, when running on a Compute Engine VM, because there the authentication is negotiated automatically by Google.
baking JSON-Keyfiles into the images(containers) is bad idea (security wise [as you said]).
You should be able to add these kind of scopes to the Kubernetes Cluster during its creation (cannot be adjusted afterwards).
Take a look at this doc "--scopes"

Resources