Flutter Firebase Functions Error: Database not defined - javascript

I've created a delete oldFiles function for my Database that deletes nodes from my chat messages. I've used the example function provided by Firebase and updated it to fit my use. My database structure is databaseName/messages/{pushId} and I've added const functions = require('firebase-functions') and const admin = require('firebase-admin') and admin.initializeApp(). Here is what I have...
exports.deleteOldItems = functions.database.ref('messages/{pushId}').onWrite(async (change) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const cutoff = (DateTime.now().millisecondsSinceEpoch - CUT_OFF_TIME);
const oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
const snapshot = await oldItemsQuery.once('value');
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
When I review my Function logs, I'm getting the following errors...
ReferenceError: DateTime is not defined
at exports.deleteOldItems.functions.database.ref.onWrite (/srv/index.js:17:18)
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
at /worker/worker.js:825:24
at
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
And my Functions are finishing with status: error. Any ideas to what may be going on?

DateTime isn't a valid JavaScript object or identifier. If you want to work with dates and times, you will need to work with Date, as you are in the line just above where you have DateTime. You should probably review the JavaScript documentation for Date to learn how it works.

Related

Increment Realtime Database count on new user with Firebase Cloud Function

* UPDATED: THIS WORKS. SEE ANSWER BELOW *
I'm trying to write a Firebase Cloud Function that increments a Realtime Database /userCount value whenever a new user is created.
I've tried the following, but am getting "TypeError: userCountRef.transaction is not a function" in incrementCountOnNewUser.
Transactions are working for my other function incrementCountOnOpen when the value of garage is set to true, but the ref is derived from the after event object.
Any suggestions on how to do this?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// const userCountRef = functions.database.ref("/userCount"); // does NOT work
const userCountRef = admin.database().ref('/userCount'); // THIS WORKS!
exports.incrementCountOnNewUser = functions.auth.user().onCreate((user) => {
return userCountRef.transaction(count => count + 1);
});
exports.incrementCountOnOpen = functions.database.ref("/garage").onUpdate(({after}) => {
const countRef = after.ref.parent.child('count');
const newValue = after.val();
return newValue
? countRef.transaction(count => count + 1)
: null;
});
It turns out that the code above works! I had switched from the commented out code (which does NOT work). I guess it didn't wait long enough for it propagate after I published, because I see it working now!
Sorry for the confusion.

Cloud function works locally, but not after deploying it

I am writing my very first Cloud Function which replaces the word "happy" with a smile.
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
exports.emojify = functions.database.ref("/messages/{pushId}/text").onWrite((change, context) => {
const original = change.before.val();
const emojified = emojifyText(original);
return admin.database().ref().set(emojified);
});
function emojifyText(text) {
let t = text;
t = t.replace(/\b(H|h)appy\b/ig, "😀");
console.log("Result:", t);
return t;
};
I found out I can test before deploying by running firebase functions:shell and doing like this:
firebase > emojify({ before: "Happy!" })
'Successfully invoked function.'
firebase > info: User function triggered, starting execution
info: Result: 😀!
info: Execution took 2949 ms, user function completed successfully
It works. However, when testing with my Android app, the logs of my function will show:
TypeError: Cannot read property 'replace' of null
at emojifyText (/user_code/index.js:15:13)
at exports.emojify.functions.database.ref.onWrite (/user_code/index.js:8:23)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:716:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I don't understand.
After some new attempts, my code is the following:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
exports.emojify = functions.database.ref("/messages/{pushId}/text").onWrite((change, context) => {
// if (!change.before.val()) { return null; }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// I tried with this and without it, but neither helped.
const original = change.after.val();
const emojified = original.replace(/\b(H|h)appy\b/ig, "😀");
return admin.database().ref("/messages/{pushId}/text").update(emojified);
});
The closest I got was to actually make it erase everything in the base, including the path messages and replace it with the written text, with the text replaced by the emoji. Something like:
But it was using set() instead of update(), which didn't show any sign of modifying anything.
const original = change.before.val(); is the data before the write. So if there was no data at the "/messages/{pushId}/text" node before you write there, the variable original will be null.
You shoud change to:
const original = change.after.val(); which is the data after the write, i.e. your new data, which you want to "emojify".
Update following your comments below
You should use the update() method (doc here), as follows:
return admin.database().ref("/messages/" + context.params.pushId + "/").update({ text: emojified });

FieldValue undefined when using functions and Firestore

I have the following function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const FieldValue = require('firebase-admin').FieldValue;
module.exports = functions.firestore
.document('students/{studentId}')
.onDelete(event => {
const student = event.data.previous.data();
const { id, semester } = student;
const classId = student.class;
const deleteObj = {};
deleteObj[id] = FieldValue.delete(); //Line 12, this is where the error orccurs
return admin
.firestore()
.collection('semesters')
.doc(semester)
.collection('students')
.doc(classId)
.update(deleteObj);
});
Every time i run it I get the following error:
TypeError: Cannot read property 'delete' of undefined
at module.exports.functions.firestore.document.onDelete.event (/user_code/deleteStudent.js:12:37)
Looking at the docs I cant really see what I am doing wrong?
// Get the `FieldValue` object
var FieldValue = require("firebase-admin").FieldValue;
// Create a document reference
var cityRef = db.collection('cities').doc('BJ');
// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
capital: FieldValue.delete()
});
Update
So, using the web equivalent seems to work: admin.firestore.FieldValue.delete(). But that seems like a bug since i'm in a nodejs environment? Can any Firebaser confirm or deny wether that is the case or not? I'd gladly file a bug report.
Turns out it was a mistake in the documentation, the correct import should have been const FieldValue = require('firebase-admin').firestore.FieldValue;
Update
It should be said that Firebase responded within hours and are correcting the docs asap.
It worked!
Require firebase-admin
const admin = require('firebase-admin')
Add this constant
const fieldValue = admin.firestore.FieldValue;
Now use
fieldValue.delete()
For more reference : otcollect.com/post
Here you can find the solution on github.
Using typescript with
"firebase-admin": "^11.0.0"
"firebase-functions": "^3.22.0"
you have to use
import { FieldValue } from 'firebase-admin/firestore'
Same for TimeStamp

Cloud Functions for Firebase: how to reference a key in a list that has been just pushed

My question is basically what to do in your cloud function, if you want to reference keys that have been generated when the client called push().
/providerApps/{UID}/ is my path to a list of appointment nodes, so each appointment node is at /providerApps/{UID}/someKey.
I need the "new item in the list", the one that was added with push(), so I thought I could order the keys and simply get the last one, but that does not work:
// (Try to) Listen for new appointments at /providerApps/{pUID}
// and store the appointment at at /clientApps/{cUID}
// cUID is in the new appointment node
exports.storeNewAppForClient = functions.database.ref("/providerApps/{UID}").onWrite(event => {
// Exit when the data is deleted.
if (!event.data.exists()) {
console.log("deletion -> exiting");
return;
}
const pUID = event.params.UID;
const params = event.params;
console.log("params: ", params);
const firstAppVal = event.data.ref.orderByKey().limitToLast(1).val();
// TypeError: event.data.ref.orderByKey(...).limitToLast(...).val is not a function
const date = firstAppVal["dateStr"];
const cUID = firstAppVal["clientUID"];
return event.data.ref.root.child("clientApps").child(cUID).child(date).set(pUID);
});
I guess I could do it on the client side with push().getKey() and allow providers to write into clientApps node, but that seems to be less elegant.
Any ideas how to do this with cloud functions?
As an illustration, my data structure looks like this:
there are provider and their clients who make appointments
Cheers
Change your trigger location to be the newly created appointment instead of the list of appointments. Then you can access the appointment data directly:
exports.storeNewAppForClient = functions.database.ref("/providerApps/{UID}/{pushId}").onWrite(event => {
// Exit when the data is deleted.
if (!event.data.exists()) {
console.log("deletion -> exiting");
return;
}
const pUID = event.params.UID;
const params = event.params;
console.log("params: ", params);
const date = event.data.child('dateStr').val();
const cUID = event.data.child('clientUID').val();
return admin.database().ref('clientApps').child(cUID).child(date).set(pUID);
});
(updated for Frank's comment)

How to compare old and new value with Cloud Functions for Firebase with .onWrite or onchange?

Lets take the following data structure:
Now I want to refresh the accessTokenFacebook with a Firebase Function.
I tested two option:
the onWrite, and the:
the onChanged
The onWrite looks the best to me, but with the following function:
exports.getFacebookAccessTokenOnchange = functions.database.ref('/users/{uid}/userAccountInfo/lastLogin').onWrite(event => {
const lastLogin = event.data;
let dateObject = new Date();
let currentDate = dateObject.toUTCString();
return lastLogin.ref.parent.parent.child('services').child('facebook').update({'accessTokenFacebook': currentDate});
});
Something happens I don'understand/can solve: when I delete a whole userUID-record (for a cleanup), the userUID-record automatically create, then only with the following path {uid}/services/facebood/accesTokenFacebook...
It seems that a deletion also triggers a onWrite.
I also tried the .onchange, but that one only triggers when there is still no accessTokenFacebook. When the change make this one, the change never triggered again.
So the next thing I want to do is a comparison between the old and new value. Do you have an example? Or is there a better solution?
UPDATE:
Cloud Functions recently introduced changes to the API as noted here.
Now (>= v1.0.0)
exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
const beforeData = change.before.val(); // data before the write
const afterData = change.after.val(); // data after the write
});
Before (<= v0.9.1)
exports.dbWrite = functions.database.ref('/path').onWrite((event) => {
const beforeData = event.data.previous.val(); // data before the write
const afterData = event.data.val(); // data after the write
});
Now that these functions are deprecated and this is the number one search result for this subject, here is the new updated version that Cloud Functions now use.
exports.yourFunction = functions.database.ref('/path/{randomPath}/goes/here').onWrite((change, context) => {
// Your value after the write
const newVal = change.after.val();
// Your value before the write
const oldVal = change.before.val();
});

Categories

Resources