Странная проблема с облачной функцией firebase console.log - PullRequest
1 голос
/ 01 ноября 2019

У меня есть ситуация в облачной функции firebase, когда выполняется только ПЕРВАЯ консоль update.log или оператор обновления firestore.

У меня есть другая похожая функция с небольшим изменением способа обработки ответа, но у него нет проблем

Я проверил консоль google cloud console / firebase и т. Д., И кажется, что исходный кодбыли загружены правильно

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' }
  });
});

Я вижу еще один пост , противоположный этой ситуации. Кто-нибудь знает, почему это происходит?

1 Ответ

0 голосов
/ 01 ноября 2019

Попробовав несколько вариантов, получите странное решение. Но работает:

Добавить любое утверждение ПОСЛЕ вызова API. В противном случае облачная функция считает, что функция окончена. С любым оператором (скажем, console.log ('dummy') после вызова API, он работает как положено

.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' }
  });

console.log('dummy'); // <-- Insert any dummy statement

});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...