简体   繁体   中英

Firebase: Calling Cloud Function From Cloud Function

I am running in to an issue with Firebase cloud functions. I have an onWrite cloud function that triggers a sequence of events. I have a path for requests that the onWrite cloud function is tied to. When that cloud function executes, it deletes the new request for the requests path and pushes the request in to a render path/que that will be used client side for rendering UI elements/data. Once the data has been written to the render path, I call a vanilla javascript function that is not tied to any cloud events. The vanilla javascript function is supposed to reach out to an external API and fetch some data to later be updated on the render object that was pushed in to the render path.

The problem is that the vanilla javascript function never executes. I have been looking all over the web to figure out why this happening but can't seem to figure out why. I am on the Flame plan so outbound api requests should be allowed to my knowledge. Here an example of my code:

 const functions = require('firebase-functions'); const admin = require('firebase-admin'); const request = require('request'); admin.initializeApp(); exports.requestModule = functions.database.ref('/requests').onWrite((change, context) => { // Create reference to database let db = admin.database(); if (context && context.auth && context.auth.uid) { const afterData = change.after.val(); let uid = context.auth.uid; let cleanData = afterData[uid]; cleanData.status = "loading"; // Remove the requested module from the requests path let cleansePath = db.ref('/requests/' + uid); cleansePath.remove().then((snapshot) => { return true; }).catch((error) => { console.log(error); return false; }); // Add requested module to the render path let renderPath = db.ref('/render/' + uid); renderPath.push(cleanData).then((snapshot) => { let val = snapshot.val(); let key = snapshot.key; // Trigger the get weather api call getWeather(uid, key, val); return true; }).catch((error) => { console.log(error); return false; }); } }); // Fetches data from external api function getWeather (uid, key, obj) { console.log('Fetching weather!'); let db = admin.database(); request('https://api.someweathersite.net/forecast/', (error, response, body) => { if (!error && Number(response.statusCode) === 200) { console.log('error:', error); console.log('statusCode:', response && response.statusCode); console.log('body:', body); obj.data = body; obj.status = 'loaded'; // Set data from api response in render object to be shown client side let render = db.ref('/render/' + uid + '/' + key ); render.set(obj).then(() => { return true; }).catch((error) => { console.log(error) return false; }); } }); } 

The console.log message at the top of the "getWeather" function never executes. I don't think that the "getWeather" function is ever executing.

If I put the api call directly in the onWrite "requestModule" function, the api call will work. However, when it calls an external function it never gets called/works. I basically want to have the "requestModule" function handle all requests and plan to have a module dispatcher that handles which module function/api data should be fetched from. That's why I don't want to keep the api call in the "requestModule" function. Any idea of why this happening or how I can get this working?

getWeather is performing asynchronous work to fetch some data, but it's not returning a promise to indicate when that work is complete. In fact, none of the async work you're performing here is correctly using the promises returned by the various API calls. It's not sufficient to simply use then() on each promise.

You need to keep track of all of the async work, and return a single promise that resolves only after all the work is complete. Otherwise, Cloud Functions may terminate and clean up your function before the work is complete. (Note that it's not deterministic which work may or may not actually complete before forced termination, but the only way to ensure that all work completes is through that single promise you return.)

You may want to watch my tutorials on using promises in Cloud Functions to get a better handle on what you're required to do make your functions work correctly: https://firebase.google.com/docs/functions/video-series/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM