Google cloud function onCreate not writing to the Database - javascript

I have created a google function for firebase that when A new conversation is added the function attaches it to the Users table under a new collection for each user in the conversation but when the function gets triggered nothing happens in the database, So far I have console logged the values tp make sure they ware being set right and they ware I have also tried looking at the google function logs and there are no errors according to the logs the script ran with no errors
Here is the code for the function
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
export const onConversationCreated = functions.firestore.document("Conversations/{conversationID}").onCreate((snapshot, context) => {
let data = snapshot.data();
let conversationID = context.params.conversationID;
if(data){
let members = data.members;
for(let index = 0; index < members.length; index++){
let currentUserID = members[index];
let remainingUserIDs = members.filter((u: string) => u !== currentUserID)
remainingUserIDs.forEach((m: string) => {
return admin.firestore().collection("Users").doc(m).get().then((_doc) => {
let userData = _doc.data();
if(userData){
return admin.firestore().collection("Users").doc(currentUserID).collection("Conversations").doc(m).create({
"conversationID": conversationID,
"image": userData.image,
"unseenCount": 1,
});
}
return null;
}).catch(() => {return null})
});
}
}
return null;
});
Can someone tell me if there is something wrong with my code or do I have to give functions permission to write to the cloud firestore database?

You are not dealing with promises correctly. Your function needs to return a promise that resolves with all of the asynchronous work is complete. Right now, it's just returning null, and not waiting for any work to complete.
All Firestore operations are asynchronous and return a promise. You will need to use these promises to build a single promise to return from the function. That means each time you query and write a document, that's generating another promise to handle.
Also, you should know that there is no method create() that you're trying to use to add a document. Perhaps you meant to use set() instead. The code will crash if it tries to execute create().

Related

Firebase cloud functions errors

Greetings of the day to everyone,
So I'm having a really hard time with Firebase cause there's just so many versions and things going on. Its extremely complicated. I wanted to achieve some functionality which is not available through the client modular web 9 version.
So I have been trying to use the Cloud functions to just get the list of all the collections in a document.
My cloud function looks like this so far -
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const { object } = require("firebase-functions/v1/storage");
admin.initializeApp();
const db = admin.firestore();
exports.addMessages = functions.https.onCall(async (data, context) => {
// Grab the text parameter.
const original = data.text;
var test;
const writeResult = admin.firestore().collection('users').doc(original)
const collections = await writeResult.listCollections();
collections.forEach(collection => {
console.log('Found subcollection with id:', collection.id);
test = collection.id;
});
// Send back a message that we've successfully written the message
return { id: test }
});
I call this function on my front end simply like this --
const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessages');
const Cloud = () => {
addMessage({ docPath: `users/${userProfile.uid}` })
.then(function (result) {
var collections = result.data.collections;
console.log(collections);
})
.catch(function (error) {
// Getting the Error details.
var code = error.code;
var message = error.message;
var details = error.details;
// ...
});
}
However, I get a 500 ERROR. I have checked the payload and everything, the data is being passed and the function is being called but there is some other kind of bug that I seem to be missing.
If anyone has any ideas please do let me know.
First off, you're calling addMessage with an Object parameter and then trying to access it's payload by the text field of data, which is undefined. You could either pass users/${userProfile.uid} as a string parameter or assign data.docPath to original.
Also, test will only contain the last collection id, since it's being overwritten forEach of the collections. So, may I suggest you make test a string array and push() each collection id to it?
Finally, on the callback of addMessage, there is no data field to access in result. In the case you decide to use an array, result will simply be the array you returned from the cloud function.
P.S. I'm not sure I see a reason why this wouldn't work with the V9 modular SDK.

React Native & Firebase RTDB - having trouble with returns

I'm using React Native with the Firebase SDK (version 9). I have a function that is supposed to check in the database to see if I have an access token stored and when it expires if so. I need the code to return true or false as a result. I think I'm getting tripped up because 'get' is asynchronous. Here's what I have currently:
export const checkToken = (useruid) => {
const db = getDatabase();
let outcome = "not set"
const userRef = ref(db,'/users/'+useruid);
get(userRef).then((snapshot) => {
console.log('snapshot is', snapshot)
let expire_time = snapshot.val().expire_time
if (expire_time > Date.now() + 180000) {
console.log('expire time is more than 3 min in the future')
outcome = true
return true
} else {
console.log('token is stale, snapshot is ',snapshot)
outcome = false
return false
}
} )
console.log('outcome is now', outcome)
}
The last console.log appears first (outcome is now not set), and if I call checkToken in another function (as console.log(checkToken(someUserID)), I get back that it is returning undefined. And then I get all the console logging with the successful snapshot retrieval.
I've read about promises, and async/await, but I'm having trouble translating that to the Firebase RTBD SDK functions.
Many thanks in advance for help/pointers!
If you just change it to:
export const checkToken = async function (useruid) {
const db = getDatabase();
let outcome = "not set"
const userRef = ref(db,'/users/'+useruid);
await get(userRef).then((snapshot) => {
.... insert rest of code
It should work as you expect.
You mention reading about promises and async/await, but you may want to read about the JS event loop, too. Basically JS doesn't wait for asynchronous actions before moving on to the rest of the code. So the way you have it structured now it kicks off the get(userRef) and then just moves on while it waits for that promise to return.

Firebase Functions: Why do they sometimes fail? Why do they often complete without error but don't fulfill all tasks?

This perplexes me. I'm six months into a firebase project and have been using Javascript for firebase-functions. I've learned a lot along the way by adding transactions, promises, batch writes and neat tricks. However, it seems like complete luck for a function to execute correctly. More often than not, the functions do execute correctly, but there are strange periods when bursts of consecutive function calls where functions half complete with no errors in the logs.
For example. I have a function for when a new user joins my app. It does a little bit of server data construction and also notifies the two admins that a new user has joined. Last night I did a test run with two new users and got no notification, but their user profiles constructed correctly on the server database. I checked the function logs and there were no errors.
Am I not handling Promises in the correct way? If a firebase function hangs, does it mess up the next few function calls?
exports.onNewUser = functions.firestore
.document('/users/{userId}')
.onCreate(async (snapshot, context) => {
user = snapshot.data().username;
//Notification payload
const payload = {
notification: {
title: `New user!`,
body: `${user} has joined [AppName]`
}
};
var promises = [];
//Check if usename unique
var passed = true;
promises.push(db.runTransaction(async t => {
const docRef = db.collection('users').doc('index');
const doc = await t.get(docRef);
var newIndex = doc.data().usernames;
if (newIndex[user.toUpperCase()] == true) {
t.delete(snapshot.ref);
passed = false;
return null;
} else {
newIndex[user.toUpperCase()] = true;
t.set(docRef, { 'usernames': newIndex });
}
}));
if (!passed) return Promise.all(promises);
//add new user to Algolia database
const algoliasearch = require('algoliasearch');
const algoliaClient = algoliasearch(functions.config().algolia.appid, functions.config().algolia.apikey);
const collectionIndex = algoliaClient.initIndex(collectionIndexName);
await saveDocumentInAlgolia(snapshot, collectionIndex);
//Notify Admins
db.collection('notificationTokens')
.doc(admin1)
.get().then((doc) => {
if (doc.exists && doc.data().notificationToken != null)
promises.push(pushNotification(doc.data().notificationToken, payload));
});
db.collection('notificationTokens')
.doc(admin2)
.get().then((doc) => {
if (doc.exists && doc.data().notificationToken != null)
promises.push(pushNotification(doc.data().notificationToken, payload));
});
return Promise.all(promises);
});
Just change
return Promise.all(promises);
to
return await Promise.all(promises);
You have to wait till the promises resolve before you return the function, as that would stop the instance of the cloud function.

Async Function in AWS Lambda Alexa Intent with Yummly API

I'm trying to retrieve data from the Yummly API through Amazon Alexa using ws-yummly in Node.js deployed on AWS Lambda. I'm fairly new to all aspects of this, but new to javascript in particular (Python is my 'native' language).
Here is what I have for my recommendation intent:
"RecommendationIntent": function () {
// delegate to Alexa to collect all the required slots
let filledSlots = delegateSlotCollection.call(this);
if (!filledSlots) {
return;
}
console.log("filled slots: " + JSON.stringify(filledSlots));
// at this point, we know that all required slots are filled.
let slotValues = getSlotValues(filledSlots);
console.log(JSON.stringify(slotValues));
const mainIngredientQuery = slotValues.mainIngredient.resolved;
async function main (queryWord) {
const resp = await Yummly.query(queryWord)
.maxTotalTimeInSeconds(1400)
.maxResults(20)
.minRating(3)
.get();
const names = resp.matches.map(recipe => recipe.recipeName);
const speechOutput = String(names[0]);
this.response.speak(speechOutput);
this.emit(":responseReady");
}
main(mainIngredientQuery).catch(error => console.error(error))
},
This is in the index.js file deployed on AWS lambda. I have isolated the problem to the async function. I have tested the function locally and it returns to console.log a list of recipe names. I want to have Alexa say these names. Or at least one name.
If I put the speechOutput assignment inside (as it is now), then I get an error that the 'Speechlet Response is set to null'.
If I tell it to 'return names' and set the external assignment to names or names[0] I get object promise or undefined (respectively).
Everything else in my program works fine and test these two bits apart they work, but putting them together doesn't work. I think that this is a syntax or placement error, but I don't understand the structure or formatting well enough yet (still learning) to understand what to try next.
How about using Promise.then like this:
async function main (queryWord) {
const resp = await Yummly.query(queryWord)
.maxTotalTimeInSeconds(1400)
.maxResults(20)
.minRating(3)
.get();
const names = resp.matches.map(recipe => recipe.recipeName);
return String(names[0]);
}
main(mainIngredientQuery)
.catch( error => console.error(error) )
.then( data => {
this.response.speak( data );
this.emit(":responseReady");
});
I'm updating this in case anyone else has the same problem.
If you notice, in my original code, I had an async function inside the intent. That didnt work because the intent itself was/is a function. By making the intent function an async function instead, I was able to solve the problem.
The following is working code for an async/await Alexa Intent.
The full index.js is available on my github if you want to take a look, but that will be a more advanced final version. The code below immediately follows up on the original question.
"RecommendationIntent": async function main () {
// delegate to Alexa to collect all the required slots
let filledSlots = delegateSlotCollection.call(this);
if (!filledSlots) {
return;
}
console.log("filled slots: " + JSON.stringify(filledSlots));
// at this point, we know that all required slots are filled.
let slotValues = getSlotValues(filledSlots);
console.log(JSON.stringify(slotValues));
const mainIngredientQuery = slotValues.mainIngredient.resolved;
const resp = await Yummly.query('chicken')
.maxTotalTimeInSeconds(1400)
.maxResults(20)
.minRating(3)
.get();
const names = resp.matches.map(recipe => recipe.recipeName);
console.log(names);
const speechOutput = names[0];
this.response.speak(speechOutput);
this.emit(":responseReady");
},

firebase functions endpoint cold starts

Firebase REST endpoint. I am getting a lot of NULL returns especially at startup. Looking through other issues, I think it is a Coldstart. I believe the issue is that I am using callbacks which is returning before firebase has a chance to return a dataset. I read a comment about callabcks from #puf - frank-van-puffelen
sugesting a Cold Start. So I'm trying to re-write as a promise. This code works, usually, but still get the cold start NULL data sets. How would I do this as a promise?
var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//=================================================================================================
// KeysFromAccountGet01
//=================================================================================================
// This function is not working correctly because it often returns a NULL set. probably
// because I am using callbacks instead of promises, and the callback returns before firebase
// can return a query. Usually it works.
// But I am fairly sure that I should be using PROMICES so as to wait for the data to arrive.
// that said, I can not figure out how to do a promise. Everythign I have tried returns nothing.
// some sugestions on how to do promises for this would be appreciated.
//curl 'https://us-central1-test.cloudfunctions.net/KeysFromAccountGet01?account=dporras8'
//curl 'https://us-central1-test.cloudfunctions.net/KeysFromAccountGet01?account='
//firebase deploy --only functions:KeysFromAccountGet01
exports.KeysFromAccountGet01 = functions.https.onRequest((req, res) =>{
var arr =[];
arr.push("1====+++starting");
arr.push("acount = "+ req.query.account);
admin.database().ref('/newacctok/'+req.query.account+'/tok3/').on('value', function(snapshot){
snapshot.forEach(function(miniSnapShot){
var tt = miniSnapShot.val();
var json = ({
"key":miniSnapShot.key,
"account":req.query.account,
"uuid":tt.uuid,
"ts2":tt.ts2,
"token":tt.token
});
arr.push(json);
})
.then(res.status(200).send(arr));
});
//===================================
I'm not sure these changes will help with your Null returns. Note that I changed on(), which leaves the listener attached, to once(). Also, I've seen answers from Frank van Puffelen cautioning against performing asynchronous processing in HTTPS request functions. I'll try to find his answers/comments and add them.
exports.KeysFromAccountGet01 = functions.https.onRequest((req, res) => {
var arr =[];
arr.push("1====+++starting");
arr.push("acount = "+ req.query.account);
// note change from on() to once()
admin.database().ref('/newacctok/'+req.query.account+'/tok3/').once('value')
.then(snapshot => {
snapshot.forEach(miniSnapShot => {
var tt = miniSnapShot.val();
var json = ({
"key":miniSnapShot.key,
"account":req.query.account,
"uuid":tt.uuid,
"ts2":tt.ts2,
"token":tt.token
});
arr.push(json);
});
res.status(200).send(arr)
});
});

Categories

Resources