I'm trying to run the following Cloud Function:
exports.getUserData = functions.firestore
.document('UserData/{id}')
.onWrite(async (snap, context) => {
const uid = snap.data.id;
let uData;
console.log("onCreate called. uid="+uid);
await admin.auth().getUser(uid)
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully fetched user data:', userRecord.toJSON());
uData = userRecord.toJSON();
})
.catch(function(error) {
console.log('Error fetching user data:', error);
});
await admin
.firestore()
.doc('UserData/${uid}')
.set({
userRecord : uData
});
return null;
});
It gets deployed allright, as I can see it in the console. But adding/updating a doc in the collection simply does not trigger the function (nothing shows in log).
A couple of things, as I see a few problems
Seems to me that you want to trigger this function every time there is a new UserData collection. If this is the case, you should use the trigger onCreate. onWrite gets triggered every time a doc is updated, created or deleted.
You function is creating an infinite loop if you use onWrite. You are updating collections which will triggered the same function, over and over.
First argument of the function is not a snapDoc, if you are using onWrite. Check the documentation
This part:
await admin
.firestore()
.doc('UserData/${uid}')
.set({
userRecord : uData
});
'UserData/${uid}' is a string not a template string. Use backtick ` not single quote '
As #renaud-tarnec said, use context.params to get the id parameter
It seems that by doing
exports.getUserData = functions.firestore
.document('UserData/{id}')
.onWrite(async (snap, context) => {
const uid = snap.data.id;
//...
});
you want to assign to the uid variable the value of the {id} wildcard in the 'UserData/{id}'.
For that you should use the context Object, as follows:
const uid = context.params.id;
and as explained here in the doc.
Related
I am using Firebase functions for Firestore database. I am trying to update a field based on the new tweet being added.
Here is my Firebase Function on production:
const admin = require('firebase-admin')
admin.initializeApp()
const db = admin.firestore()
const functions = require("firebase-functions");
functions.logger.log("START OF FUNCTION");
exports.myFunction = functions.firestore
.document('timelines/{userId}/tweets/{tweetId}')
.onCreate((change, context) => {
const userId = context.params.userId
const tweetId = context.params.tweetId
functions.logger.log(context.params.userId);
functions.logger.log(context.params.tweetId);
db.doc(`/timelines/${userId}/tweets/${tweetId}`).update({likeCount: 200})
})
I am triggering it through an iPhone app. I am logged in to my account and I add a new Tweet. The Firebase function does get called but userId and tweetId are undefined. I am not sure why they are undefined. Any ideas?
Without knowing your client-side logic it's difficult to know if there are other issues. I would suggest adding some error handling to narrow down the cause. You could also try pulling it from the data response instead of context (assuming the schema matches).
Also note using 'snap' instead of 'change' as change is generally reserved for 'onWrite' and 'onUpdate' hooks.
exports.myFunction = functions.firestore
.document('timelines/{userId}/tweets/{tweetId}')
.onCreate(async (snap, context) => {
try {
const { userId, tweetId } = snap.data();
functions.logger.log(userId);
functions.logger.log(tweetId);
return await db.doc(`/timelines/${userId}/tweets/${tweetId}`).update({ likeCount: 200 });
}
catch (error) {
functions.logger.log(error);
}
});
My current code uses:
var currentUID = await database.getCurrentUserID();
Running this function with await on this line of code stores data in Firestore with correct user ID but time is always set to 0:
Future<void> addUserTime() async {
var currentUID = await database.getCurrentUserID();
return await database.workoutCollection
.doc(currentUID.toString())
.set({
'Workout Time': format(duration),
})
.then((value) => print('Time added'))
.catchError((error) => print('Failed to add time to database'));
}
Without using await like the previous line of code like this:
var currentUID = database.getCurrentUserID();
Firestore shows this: This is the firebase output. Wrong UserID from Firebase Authentication, but time is always set to what the user logged:
Future<void> addUserTime() async {
var currentUID = database.getCurrentUserID();
return await database.workoutCollection
.doc(currentUID.toString())
.set({
'Workout Time': format(duration),
})
.then((value) => print('Time added'))
.catchError((error) => print('Failed to add time to database'));
}
This is my database class where I call the getCurrentUserID() function:
How can I get both the correct UID and correct time the user logged?
FirebaseAuth stores the current user once it's authenticated in FirebaseAuth.instance.currentUser, if we look into this property we will find that the type is User?, not Future<User?>, hence you don't need to await to get the currentUser, simply:
return FirebaseAuth.instance.currentUser?.uid;
Additionally, currentUser?.uid returns a String, so no need to call .toString().
Assuming that the duration is not 0, with these modifications the code should work, for further reference here's a DartPad example that updates a user record based on currentUser.uid.
So, I don't really know how to write JS, I am developing a mobile app in Flutter, and I would be grateful for some help and clarifications regarding Future/Promises in JS.
I got a collection of posts for each user, and I want to create an .onCreate function which when a user posts a new post (a new document is created inside the 'posts/userId/user_posts' collection), then it gets all the user's followers (from a collection 'user_followers/userUid') and for each follower, it writes the postUid and postOwnerUid to that follower's newsFeed collection ('user_news_feed/followerId').
This is what I got right now, but I am walking blind, since I really don't know JS and I don't know how can I await a write function while inside a get function.
And how do I prevent Cloud Timeouts? If for instance the user has 1000 followers, how can I prevent Firebase from shutting down my function and making sure all the followers are notified?
exports.writeToUserNewsFeed = functions.firestore
.document('posts/{userId}/user_posts/{postId}')
.onCreate((snap, context) => {
const postData = snap.data();
const postUid = postData['post_uid'];
const userUid = postData['user_uid'];
const postCreationDate = postData['post_creation_date'];
var docRef = db.collection('user_followers').doc(userUid).collection('followers');
docRef.get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
db.collection('user_news_feed')
.doc(doc.data['uid'])
.collection('feed')
.document(postUid)
.set({
'post_uid': postUid,
'user_uid': userUid,
'post_uid': postCreationDate,
});
});
});
});
As explained in the doc, in a background Cloud Function like an onCreate() for Firestore, you need to return a Promise when all the asynchronous work is completed. So in your case, one possibility is to use Promise.all() because you don't know upfront how many documents are in the followers subcollection. Since Promise.all() returns a single Promise you can include it in the Promise chain that you need to return in the Cloud Function.
exports.writeToUserNewsFeed = functions.firestore
.document('posts/{userId}/user_posts/{postId}')
.onCreate((snap, context) => {
const postData = snap.data();
const postUid = postData['post_uid'];
const userUid = postData['user_uid'];
const postCreationDate = postData['post_creation_date'];
var followersColRef = db.collection('user_followers').doc(userUid).collection('followers');
return followersColRef.get().then((querySnapshot) => { // <= See return here
const promises = [];
querySnapshot.forEach((doc) => {
promises.push(
db.collection('user_news_feed')
.doc(doc.data['uid'])
.collection('feed')
.doc(postUid)
.set({
'post_uid': postUid,
'user_uid': userUid,
'post_uid': postCreationDate,
})
);
});
return Promise.all(promises); // <= See return here
})
.catch(error => {
console.log(error);
return null;
})
});
Note that instead of using Promise.all() you could also use a batched write but there is a limit of 500 operations for a batched write.
I am trying to write a firebase cloud function that attaches a payment method to a stripe customer, subscribes them to a plan, and writes the subscription object to firestore.
I actually just wrote a function that worked but am not sure what I changed.
exports.attachAndSubscribe = functions.firestore
.document('stripe_customers/{userId}')
.onUpdate(async (change, context) => {
const source = change.after.data();
const paymentMethod = source.paymentMethod;
await stripe.paymentMethods.attach(paymentMethod.id,
{customer: source.customer_id},
{invoice_settings: {default_payment_method: paymentMethod.id}
});
const subscription = await stripe.subscriptions.create(
{customer: source.customer_id,items: [{plan: 'plan_FnA3IsFL5Xc6Ct'}]
});
return admin.firestore()
.collection('stripe_customers')
.doc(userId)
.set(
{subscription: subscription});
});
When the function gets triggered I get the following error:
Stripe: Unknown arguments ([object Object]). Did you mean to pass an
options object?
duck was right. The default_payment_method property only exists in the customer object, not in the paymentMethod object. I solved the problem by separately updating the stripe customer after attaching the payment method.
I have created a firebase function that listen on onCreate event, however the DocumentSnapshot.data() is returning empty.
The function code is:
exports.createClientAccount = functions.firestore
.document('/userProfile/{userId}/clientList/{clientId}')
.onCreate(async (snap, context) => {
console.log('****snap.data(): ', snap.data()); //Showing Empty from the console.
return admin
.auth()
.createUser({
uid: context.params.clientId,
email: snap.data().email,
password: '123456789',
displayName: snap.data().fullName,
})
.then(userRecord => {
return admin
.database()
.ref(`/userProfile/${userRecord.uid}`)
.set({
fullName: userRecord.displayName, //ERROR here: Reference.set failed: First argument contains undefined
email: userRecord.email,
coachId: context.params.userId,
admin: false,
startingWeight: snap.data().startingWeight,
});
})
.catch(error => {
console.error('****Error creating new user',error);
});
});
The document IS created on the firebase database under
/userProfile/{userId}/clientList/{clientId}
clientId document created on the database
As per the documentation, onCreate listens when a new document is created and returns the snapshot of the data created through the DocumentSnapshot interface. However, I have checked from the console that snap.data() is empty. I don't understand why it is empty if the document is created successfully on the database.
image showing error returned by the functions when creating the userProfile
From the function code, return admin.auth.createUser({}) is creating the user as anonymous because snap.data().email is undefined, but it should create a non anonymous user.
First, please try to change document('/userProfile/{userId}/clientList/{clientId}') to document('userProfile/{userId}/clientList/{clientId}').
path should not start with /.
exports.createClientAccount = functions.firestore
.document('userProfile/{userId}/clientList/{clientId}')
At the end problem was that when I created the document with add({}) I was not including the fields in the instruction. This is the function that creates the client document and now the function gets triggered correctly.
async clientCreate(
fullName: string,
email: string,
startingWeight: number
): Promise<any> {
const newClientRef = await this.firestore
.collection(`userProfile/${this.userId}/clientList/`)
.add({
fullName,
email,
startingWeight: startingWeight * 1,
});
return newClientRef.update({
id: newClientRef.id
});
}
I was calling this.firestore.collection(...).add({}) with no fields, so when it happened, the cloud function got triggered and the DocumentSnapshot.data() was empty thus returning the Reference.set error. The cloud function createClientAccount is correct. Thanks.