1

I have a situation in firebase cloud function where only FIRST console.log or firestore update statement is executed.

I have other similar function with slight variation in the way response is processed, but that does not have any issues

I have checked google cloud console/firebase console etc and the source code seem to have been uploaded correctly

exports.myFunction = functions.firestore.document(docPath).onCreate(async (snapshot, context) => {

  var inputData = snapshot.data();
  console.log('printing request');
  console.log(inputData);

  //Prepare to call the api
  var data = {'input': 'some value'};
  var resource = 'api_resource';

  // call api - below function is provided by an external provider and takes a callback function once api call is complete

  myApi.call(resource, data.input, function (error, result) {
    if (error) {
      console.log('Error from api');
      return { status: 'error', code: 401, message: 'Error from api' }
    }

    var apiResult = JSON.parse(result);
    console.log('printing api response'); // <-- Anything below this does not get executed. When this is removed, next line is executed and so on 
    console.log(apiResult);  

    //Write to Firestore
    snapshot.ref.update({
      'status': 'computed',
    })
      .then((a) => {
        console.log('Written successfully to db');
        return 0;
      })
      .catch(err => {
        console.log('Error setting db' + err);
        return { status: 'error', code: 401, message: 'Error setting db ' }
      });

    // console.log('End of function');
    return { status: 'success', code: 200, message: 'Completed successfully' }
  });
});

I see another post that is opposite of this situation. Anyone know why this happens?

2
  • I suspect something is happening with your handling of the callback, since background activities are not supported by Cloud Functions, unless you actually return a promise. Can you promisify the API you are calling and return the whole chain? Basically, you can expect processing to stop shortly after that api call is made when the top level function returns. Commented Oct 31, 2019 at 22:21
  • @robsiemb Thank you for the response. I had same hunch, except same api call with a similar callback function has no issue executing. Let me try your suggestion Commented Oct 31, 2019 at 22:46

1 Answer 1

2

You need to wrap your synchronous API in a Promise - details here and here.

Code might look something like:

exports.myFunction = functions.firestore.document(docPath).onCreate( (snapshot, context) => {

  var data = 'some value'
  var resource = 'api_resource';

  const myAPIPromise = (resource, data) => {
    return new Promise((resolve, reject) => {
      myApi.call(resource, data, (error, result) => {
        if (error) reject(error)
        else resolve(result)
      });
    })
  }

  myAPIPromise(resource, data)
  .then((result) => console.log(result))
  .catch((err) => console.log(err))

});

You'll obviously want to add back in all extraneous logs and call to Firestore which I removed for clarity.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I have removed my partially correct and works-with-chance answer :)
Thanks. Hopefully now another beginner with a similar problem will be less confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.