how to get particular field data from cloud firestore in node.js (Cloud Functions) and push in particular field via cloud Functions

Viewed 198

I want to create a cloud function it will trigger when a user is create image then add a additional field named wallet but from cloud functions and it's value become 0 like this wallet image at the same place but I can't understand how can node.js can get the user mobile number to add this field because it will run once per user because when user will recharge it will not execute.

How can I achieve that? here's my code:

My Code was not worked

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const db = admin.firestore();

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
// firebaseUser.initializeApp(functions.config().firebase);

exports.onUserCreate = functions.firestore  
  .document("Users/{mobile}")
  .onCreate(async (snap, context) => {
    const values = snap.data();

// send wallet value to the fields
await db.collection("Users").document(values.Mobile).add({
  Wallet: `0`,
});
});
1 Answers

You can use Google Cloud Firestore Triggers to call cloud function. You have several types of triggers like create, update, delete, and write If you want to call cloud function every time when the user document is created, you can use providers/cloud.firestore/eventTypes/document.create which is a default trigger. If you want to call the cloud function whenever a document gets updated, you can use providers/cloud.firestore/eventTypes/document.update. Currently, there are no triggers which you can use whenever a particular field gets updated. So you have to implement this logic in the cloud function that whenever you update a document, it checks for the particular field or you can run the function whenever a new user document is created.

I Currently there are no triggers for a specific field. You can only trigger on any change to a document, and if you want to know if a particular field changed, you have to examine the change object to figure it out.

You can always file a feature request if you would like to be able to express a function that only pays attention to a particular field.

Related