Why do I get this Cloud Firestore Function error? - javascript

I get this error in the Cloud Firestore Function log view.
I'm new to JavaScript and Firestore and could use some advice on this
TypeError: event.data.previous.data is not a function at
exports.onVisitorPres...
exports.onVisitorPresenceWrite = functions.database
.ref("/VISITORS_PRESENCE/{uid1}/{uid2}").onWrite((event) => {
// Get the data written or deleted on the Realtime Database
var eventStatus = event.data.val();
const previousData = event.data.previous.data();
// If the onWrite event is a delete event then use previousData
if(eventStatus == null){
eventStatus = previousData;
}
});
What I want to do is using the .onWrite((event) on a Firebase Realtime databas key and Firestore Function trigger when the key items get deleted and when new items are added.
I thought I could check the eventStatus == null and that is when data is deleted I simply use the previousData (before it got delete??)

The event variable is an instance of Event and therefore calling event.data will return a DeltaDocumentSnapshot for Firestore and a DeltaSnapshot for the Realtime Database.
With a these delta snapshots, you can obtain the previous value with previous which will return another DeltaDocumentSnapshot or DeltaSnapshot containing the previous state from before the write event was triggered.
In your example, you're using a Firebase Realtime Database trigger, which means event.data.previous will return a DeltaSnapshot, which does not support data(), but instead supports val().
In this case, as you've done with var eventStatus = event.data.val();, you need to call val() on previous:
const previousData = event.data.previous.val();
If instead you are trying to use a Firestore trigger, you need to change this:
functions.database.ref("[...]")
To this:
functions.firestore.document("[...]")
And then you can use event.data.previous.data().

Related

How to access snapshot of Firebase database within Firebase Function?

Every hour, I want my firebase function to look through my database, read a value, calculate a new value from this old value, and then update it in the database. I am having trouble accessing a snapshot of the data. Specificically,
exports.scheduledFunction = functions.pubsub.schedule('every 1 hour').onRun((context) => {
const ref = functions.database.ref('/users/test_user/commutes');
ref.once('value',function(snapshot) {
// do new calculation here
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
return null;
});
I am getting a : functions: TypeError: ref.once is not a function error.
How do I access a value from my firebase real time database and update it from a Firebase function?
You're trying to use the firebase-functions SDK to query the database. It can't do that. You will have to use the Firebase Admin SDK to make the query.
You will need to get started like this (not complete, but you should be able to see what you need to do). Import and initialize at the global scope:
const admin = require('firebase-admin')
admin.initializeApp()
Then in your function, use it. Be sure to work with promises correctly.
const ref = admin.database().ref('...')
return ref.once('value').then(snapshot => {
// work with the snapshot here, and return another promise
// that resolves after all your updates are complete
})
The firebase-functions is different from the client side. The ref() function according to the docs:
ref: function
ref(path: string): RefBuilder
Select Firebase Realtime Database Reference to listen to.
Path of the database to listen to.
Returns RefBuilder
The RefBuilder will contain the database triggers that you can call, onCreate(), onWrite(). To be able to use your database, then you need to use the admin sdk.
https://firebase.google.com/docs/reference/functions/providers_database_.refbuilder

Update another location onUpdate with firebase cloud functions [duplicate]

This question already has answers here:
Firebase DB - How to update particular value of child in Firebase Database
(3 answers)
Closed 4 years ago.
I am trying to keep some data consistent with firebase cloud functions. When data changes in the main list, I want all the data to change in the user's favourite list.
Currently, I am able to call the function and get a log of the correct data which is changing, but my query doesn't work. I am getting the following error:
TypeError: ref.update is not a function
at exports.itemUpdate.functions.database.ref.onUpdate
Here is my code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.itemUpdate = functions.database
.ref('/items/{itemId}')
.onUpdate((change, context) => {
const before = change.before.val(); // DataSnapshot after the change
const after = change.after.val(); // DataSnapshot after the change
console.log(after);
if (before.effects === after.effects) {
console.log('effects didnt change')
return null;
}
const ref = admin.database().ref('users')
.orderByChild('likedItems')
.equalTo(before.title);
console.log(ref);
return ref.update(after);
});
I'm not to sure where I am going wrong, I appreciate all the help and guidance to resolve this!
Cheers.
equalTo() returns a Query object. You're then trying to call update() on that object. Note that in the linked API docs, Query doesn't have an update() method. You can't simply "update" a Query that hasn't been performed. You're going to have to actually perform the query using once(), iterate the results form the snapshot in the returned promise, and perform further updates using the data you find.

Loop while sending notification using Firebase Cloud Functions

I've been struggle this for a long time and i really have no idea why this happens.
I'm using Cloud functions to send notification base on change in status. this is my code :
exports.sample = functions.database.ref('users/{pushID}/status').onUpdate(Snap => {
const key = Snap.params.pushID;
var ref = admin.database().ref('users');
return ref.child(`${key}`).on("value", function(snap) {
const category = snap.child('category').val();
console.log("category",category);
var ref = admin.database().ref('/categories/')
return ref.child(`${category}`).on("value", function(snapshot){
const tokenID = snapshot.child('tokenID').val();
const payload = {
data: {
title: 'HELLO',
text: `HOW ARE YOU ?`
}
};
return admin.messaging().sendToDevice(tokenID, payload);
})
});
After i deploy the function i change the status using the dashboard.
When i do this i get notification! (and i get "sent" on the log)
but when i change the "status" again, i get two notifications! and now i got two times "sent" on the log and so on.
It seems like a loop but i don't understand why,
whatever i log i get only one value, i don't get multiple tokens or anything, it just sends the same notification to the same user.
Whenever i re-deploy the function the "counter" resets and then the first change send only one notification.
my database looks like this :
users
-->push id
---->status=""
---->category="test"
categories
-->test
Your listeners should be attached using once() instead of on(). on() leaves the listener attached.

How to prevent 'value' event on the client that issued set?

A Firebase client calling set() will cause all connected clients to have value triggered - including - the original client that issued the set().
In my case (and I think in most cases), there is no reason for the client that issued the set() to respond to the value event produced by its own call. Obviously its model is correct and there's no need to change it (which may be an expensive operation).
Is there any way for the client to not-receive/prevent/ignore the value event triggered by its own set() call ? I considered using off/on around set() but that can make the client miss value events that came at the same time but were not triggered by it.
Am I missing something obvious ?
Most applications treat the Firebase data itself as their model. So when there's an update, they call ref.set() (or another mutator function) and then the update flows back into their app through an on() event. React/Flux aficionados know this as a unidirectional data-flow, other might know it as Command Query Responsibility Segregation.
But there indeed cases where the model has already been updated and thus you want to ignore the event from Firebase if you're the one who triggered it.
There is no API for not receiving theses self-triggered events. Instead you'll have to "remember" the data that you sent to Firebase and filter it out in your on() handler.
The Android drawing sample from Firebase keeps a list of segments that it sends to Firebase and then ignores those segments in its onChildAdded handler. It uses push ids to identify the line segments and those are generated client-side, so it can use those to track identify the segments.
A JavaScript sample of this:
var pendingChildIds = []; // Push ids of nodes we've sent to the server, but haven't received in `on()` yet
// this code is in your UI event handler, or whatever triggers the needs to update your Firebase data
var newChild = ref.push();
pendingChildIds.push(newChild.key());
newChild.set(
{ property1: 'value1', property2: 3.14 },
function(error) {
// the write operation has completed, remove the child id from the list of pending writes
pendingChildIds.splice(pendingChildIds.indexOf(newChild.key());
}
);
// this is the event handler, using child_added in this case
ref.on('child_added', function(snapshot) {
if (!pendingChildIds.contains(snapshot.key())) {
// this is a child that we DIDN'T generate
}
});
I ended up adding a client ID to the model, something like:
var clientId=(Math.random()*10000000000000000).toFixed(0);
function set(data) {
ref.set(JSON.stringify({ clientId: clientId, data: data }));
}
ref.on('value', function(snapshot) {
var json=JSON.parse(snapshot.val());
if (!json || json.clientId===clientId) return;
var data=json.data;
// update model with data
});

Firebase snapshot reference won't return data

I'm using firebase and have a list of people, with certain priorities. In one of my functions, setting and getting priorities is working fine. But in another, I can only set and trying to get the priority of the item returns 'dataSnapshot.getPriority() is not a function'.
var playersList = new Firebase('https://myfirebase.firebaseIO.com/players')
var winnerSnapshot = playersList.child(winner);
winnerSnapshot.setPriority('1300'); //This is working
var oldPriority = winnerSnapshot.getPriority(); //Not working
There are actually two different types of object at play here. A Firebase reference, and a DataSnapshot. When you call new Firebase(), you get a Firebase reference which allows you to write data (e.g. using set or setPriority) or attach callbacks for reading data (e.g. using on or once).
These callbacks registered with on() or once() receive the data via a DataSnapshot and you can call .getPriority() on that. Check out the Reading Data docs for full details.
For example, to make your example work, you could do something like:
var winner = "somebody";
var playersListRef = new Firebase('https://myfirebase.firebaseIO.com/players')
var winnerRef = playersListRef.child(winner);
// You use a firebase reference to write data.
winnerRef.setPriority('1300');
// You can also use a firebase reference to attach a callback for reading data.
winnerRef.once('value', function(winnerSnapshot) {
// Inside your callback, you get a DataSnapshot that gives you access to the data.
var priority = winnerSnapshot.getPriority();
});

Categories