I am writing a Firebase database trigger function to push notifications to several users. For consistency I would like to batch all write operations, but I am having trouble creating the batch.
How can I get a reference to the database from data snapshot?
const functions = require('firebase-functions')
const admin = require('firebase-admin')
exports.onNoteCreate = functions
.region('europe-west1')
.database
.ref('/notes/{noteId}')
.onCreate((snapshot, context) => {
//Get a reference to the database - this does not work!
const db = snapshot.getRef()
...
const notificationObject = {"test": true}
//Run database batched operation - prepare batch
let batch = db.batch()
peopleToAlert.forEach((personId, index) => {
//Write notification to all affected people
const notificationId = db.ref().push()
const batchWrite = db.collection(`/notifications/${personId}/notes`).doc(notificationId)
batch.set(batchWrite, notificationObject)
})
//Commit database batch operation
return batch.commit().then(() => {
return new Promise( (resolve, reject) => (resolve()))
}).catch( (err) => {
return new Promise( (resolve, reject) => (reject(err)))
})
})
I have also tried the approach below to no avail
const db = admin.database()
Any clarification much appreciated! Kind regards /K
To get the root Reference of the Database from a DataSnapshot, do as follows:
const snapshotRef = snapshot.ref.root;
See https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#ref and https://firebase.google.com/docs/reference/js/firebase.database.Reference.html#root
HOWEVER, you are triggering your Cloud Function with a Realtime Database trigger while the concept of batched write is for Firestore, which is a different database service. So you cannot use the root Reference of the Realtime Database to create a Firestore WriteBatch.
So if you want to create a WriteBatch within your Cloud Function you need to get it from the Admin SDK, as follows:
let batch = admin.firestore().batch();
See https://firebase.google.com/docs/reference/admin/node/admin.firestore
Related
Problem:
An entire field of my MongoDB's collections' is not transmitted correctly from the db to the react client. For an exemple, the first element of this field is tweet_id: 1537466989966413825 in my DB. It becomes tweet_id: 1537466989966413800 when I fetch from my client.
My steps
I pull my data from the MongoDB using their App services' function like that:
exports = function(){
var collection = context.services.get("mongodb-atlas").db("meme_news").collection("news");
return collection.find({});
};
When I press the Run button of their built-in terminal, the correct data is displayed.
On my react's application, I perform the fetch like that:
useEffect(() => {
getData();
}, []);
const getData = async () => {
let getAllData = await user.functions.getAllData();
// all these data are wrong
let tweetId = getAllData
.map((ele) => {
return ele.tweet_id;
})
let tweetIdFirstEle = tweetId[0];
// this return 1537466989966413800
// It should return 1537466989966413825
};
Why is my async/await altering my Mongodb data? I have no idea what is going on here.
Im working with React native and react-native-firebase
My objective is to add multiple docs(objects) to a collection at once.
Currently, I have this:
const array = [
{
name: 'a'
},{
name: 'b'
}
]
array.forEach((doc) => {
firebase.firestore().collection('col').add(doc);
}
This triggers an update on other devices for each update made to the collection.
How can I batch these docs together for ONE update?
You can create batch write like
var db = firebase.firestore();
var batch = db.batch()
in you array add updates
array.forEach((doc) => {
var docRef = db.collection("col").doc(); //automatically generate unique id
batch.set(docRef, doc);
});
finally you have to commit that
batch.commit()
You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.
var db = firebase.firestore();
var batch = db.batch();
array.forEach((doc) => {
batch.set(db.collection('col').doc(), doc);
}
// Commit the batch
batch.commit().then(function () {
// ...
});
Version 9 of Web API is slightly different, the docs include this example:
import { writeBatch, doc } from "firebase/firestore";
// Get a new write batch
const batch = writeBatch(db);
// Set the value of 'NYC'
const nycRef = doc(db, "cities", "NYC");
batch.set(nycRef, {name: "New York City"});
// Update the population of 'SF'
const sfRef = doc(db, "cities", "SF");
batch.update(sfRef, {"population": 1000000});
// Delete the city 'LA'
const laRef = doc(db, "cities", "LA");
batch.delete(laRef);
// Commit the batch
await batch.commit();
The batch from database also has a create function that adds a new document in a collection and throws an error if there is already a document. we just need the reference to the document. Please note that this function exists in admin sdk of firebase.
const batch = db.batch();
await users.map(async (item)=> {
const collectionRef = await db.collection(COLLECTION_NAME).doc();
batch.create(collectionRef, item);
});
const result = await batch.commit();
A batched write can contain up to 500 operations. Each operation in the batch counts separately towards your Cloud Firestore usage.
Note: For bulk data entry, use a server client library with parallelized individual writes. Batched writes perform better than serialized writes but not better than parallel writes. You should use a server client library for bulk data operations and not a mobile/web SDK.
here's the basic premise of what im trying to accomplish here. if a user ask a question about a product i want to send a notification to other users who currently own that product. basically saying "hey, so and so has a question about this product. maybe you can help since you own it already"
each userProfile collection has a subcollection called 'notify' where notifications are stored for various things. what i need to do is sort through the userProducts and find every user who owns the product and then create a notification post in only the notify sub-collections for those specific users who own that product.
here is the basic code. the first bit works in that it does return an array of userIDs who own that product. where im struggling now is getting it to create a new doc in the Notify sub-collection for just those specific users. is this possible to do?
exports.Questions = functions.firestore
.document("/userPost/{id}")
.onCreate(async (snap, context) => {
const data = snap.data();
if (data.question == true) {
const userProducts = await db
.collection("userProducts")
.where("product", "==", data.tag)
.get();
const userData = userProducts.docs.map((doc) => doc.data().userId);
await db
.collection("userProfile")
.where("userId", "in", userData)
.get()
.then((querySnapshot) => {
return querySnapshot.docs.ref.collection("notify").add({
message: "a user has asked about a product you own",
});
});
});
Your current solution is on the right track, but there are improvements that can be made.
Use a guard pattern for the data.question == true check.
You don't need to get userProfile/<uid> as you aren't using its contents.
When changing multiple documents at once, you should consider batching them together for simpler error handling.
ref.add(data) is shorthand for ref.doc().set(data) which you can use in the batched write to create new documents.
exports.Questions = functions.firestore
.document("/userPost/{id}")
.onCreate(async (snap, context) => {
const data = snap.data();
if (!data.question) {
console.log("New post not a question. Ignored.")
return;
}
const userProducts = await db
.collection("userProducts")
.where("product", "==", data.tag)
.get();
const userIds = userProducts.docs.map(doc => doc.get("userId")); // more efficient than doc.data().userId
// WARNING: Limited to 500 writes at once.
// If handling more than 500 entries, split into groups.
const batch = db.batch();
const notificationContent = {
message: "a user has asked about a product you own",
};
userIds.forEach(uid => {
// creates a ref to a new document under "userProfile/<uid>/notify"
const notifyDocRef = db.collection(`userProfile/${uid}/notify`).doc();
batch.set(notifyDocRef, notificationContent);
});
await batch.commit(); // write changes to Firestore
});
Note: There is no special handling here for when no one has bought a product before. Consider pinging the product's owner too.
I have a script in Reactjs that get data (numbers) from api and addup this numbers with numbers from Firebase collection when user opens the page and the user can see this numbers.
There are going to be many users in the app and every user is going to have diffrent numbers from the same script
I was wondering if its possible with Firebase Cloud Functions to run this Client side script on the server and do the callculations of this numbers on the server and store this numbers in a Firestore collection.
im a begginer in nodejs and cloud functions i dont know if this is possible to do
get the numbers from Api
getLatestNum = (sym) => {
return API.getMarketBatch(sym).then((data) => {
return data;
});
};
Cloud function i was trying
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.resetAppointmentTimes = functions.pubsub
.schedule('30 20 * * *')
.onRun((context) => {
const appointmentTimesCollectionRef = db.collection('data');
return appointmentTimesCollectionRef
.get()
.then((querySnapshot) => {
if (querySnapshot.empty) {
return null;
} else {
let batch = db.batch();
querySnapshot.forEach((doc) => {
console.log(doc);
});
return batch.commit();
}
})
.catch((error) => {
console.log(error);
return null;
});
});
It is indeed possible to call a REST API from a Cloud Function. You need to use a Node.js library which returns Promises, like axios.
It's not 100% clear, in your question, to which specific Firestore doc(s) you want to write, but I make the asumption it will be done within the batched write.
So, something along the following lines should do the trick:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
admin.initializeApp();
const db = admin.firestore();
exports.resetAppointmentTimes = functions.pubsub
.schedule('30 20 * * *')
.onRun((context) => {
let apiData;
return axios.get('https://yourapiuri...')
.then(response => {
apiData = response.data; //For example, it depends on what the API returns
const appointmentTimesCollectionRef = db.collection('data');
return appointmentTimesCollectionRef.get();
})
.then((querySnapshot) => {
if (querySnapshot.empty) {
return null;
} else {
let batch = db.batch();
querySnapshot.forEach((doc) => {
batch.update(doc.ref, { fieldApiData: apiData});
});
return batch.commit();
}
})
.catch((error) => {
console.log(error);
return null;
});
});
Two things to note:
If you want to add the API result to some fields value, you need to give more details on your exact need
Important: You need to be on the "Blaze" pricing plan. As a matter of fact, the free "Spark" plan "allows outbound network requests only to Google-owned services". See https://firebase.google.com/pricing/ (hover your mouse on the question mark situated after the "Cloud Functions" title)
I am trying to write a Firebase Cloud function which would write the current time inside the database whenever called:
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
exports.pushDateOfCall = functions.https.onRequest((req, res) => {
const currentTime = new Date();
return admin.database().ref('/dates').push({currentTime: currentTime}).then((snapshot) => {
return res.send("Complete");
}).catch((error) => res.send("Something went wrong"));
});
After deploying the function and calling it from the function's URL, nothing is written inside the database.
Output of firebase functions logs:
Function execution took 1358 ms, finished with status code: 304
P.S. I am running the link from incognito since I wish that whoever calls the link (both authorised and unauthorised) is able to use it.
const currentTime = new Date();
Here currentTime is an object. If you want to store the String of the date, use String(currentTime) as
return admin.database().ref('/dates').push({currentTime: String(currentTime)})
#hkchakladar is right, changing to {currentTime: String(currentTime)} will solve the problem.
However, note that you don't need to return res.send() nor to return the promise returned by the asynchronous push() method. This is shown in the official Firebase video about HTTP Cloud Function, see https://www.youtube.com/watch?v=7IkUgCLr5oA
So your code may be as follows:
exports.pushDateOfCall = functions.https.onRequest((req, res) => {
const currentTime = new Date();
admin
.database()
.ref('dates')
.push({ currentTime: String(currentTime) })
.then(ref => {
res.send('Complete');
})
.catch(error => res.status(500).send('Something went wrong'));
});