Title: Firestore navigation for onUpdate and shuffle an array
Tags: reactjs;firebase;react-native;google-cloud-firestore;google-cloud-functions
Question: I have this Firebase structure:
Firebase Structure.
Then I have a function in my Code, which adds a map called &quot;set&quot;.
My Structure is looking like this after: New structure.
Now i want an onUpdate Firebase function, which is called after the map &quot;set&quot; is added in any document.
This function should shuffle my &quot;question&quot; array.
I tried something like this:
exports.shuffleSet = functions.firestore
.document('duell/{duell_id}/set/questions')
.onUpdate((change, context) =&gt; {
const data = change.after.data();
const previousData = change.before.data();
if (data.name == previousData.name) {
return null;
}
//shuffle code here
});
But Im not sure if .document('duell/{duell_id}/set/questions') is the correct way to  navigate to the question array. And at the beginning the &quot;set&quot; is not even existing as explained at the top.
How do I navigate to the question array correctly, that I can pull it &amp; update it shuffled?


Here is another answer: You should pass a document path to functions.firestore.document(). You cannot pass a field name, since Firestore Cloud Functions are triggered by documents events.
So you should do as follows:
exports.shuffleSet = functions.firestore
.document('duell/{duell_id}')
.onUpdate((change, context) =&gt; {
if (!change.after.data().shuffledSet) {
const data = change.after.data();
const question = data.set.question;  // get the value of the question field
const shuffledSet = shuffle(question);  // generate the new, suffled set. It’s up to you to write this function
return change.after.ref.update({shuffledSet});
} else {
return null; // Nothing to do, the shuffled field is already calculated
Comment for this answer: Hello @karste, did you have time to look at the answer? I see in your new question that you now do `exports.shuffleSet = functions.firestore.document('duell/{duell_id}').onCreate((snap, context) => {...});`
Comment for this answer: @karste "Set the "set" directly at the creation of the document." => this makes sense. If my answer above gave you the solution for your **initial** problem, i.e. "You should pass a document path to `functions.firestore.document()`." (and not a path to a field), please accept it. This way others will know it solves the problem.
Comment for this answer: Hey, @Renaud thanks for your answer. I noticed, its easier for me and more comfortable to just set the "set" directly at the creation of the document. Could you please have a look at my new question? Maybe you can find the mistake :)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:06/07/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<reactjs><firebase><react-native><google-cloud-firestore><google-cloud-functions>', 'Title': 'Firestore navigation for onUpdate and shuffle an array', 'ViewCount': 129, 'id': 67873539, 'user_AboutMe': None, 'user_AccountId': 21862694.0, 'user_DisplayName': 'karste', 'user_Reputation': 27.0, 'user_Views': 23.0, 'user_id': 16153315.0}}