【问题标题】:Vue and Firebase Issue: Error: function crashed out of request scope Function invocation was interruptedVue 和 Firebase 问题:错误:函数在请求范围外崩溃 函数调用被中断
【发布时间】:2018-02-13 19:07:00
【问题描述】:

我看到有人问过这个问题,但没有很好的答案。我可以理解问题是什么,但似乎无法找到解决方法。这是我的功能:

//set JSON content type and CORS headers for the response
response.header('Content-Type','application/json');
response.header('Access-Control-Allow-Origin', '*');
response.header('Access-Control-Allow-Headers', '*');

//respond to CORS preflight requests
if (request.method == 'OPTIONS') {
response.status(204).send('');
}

// pull in firebase
var firebase = require('firebase');

require("firebase/firestore");

let config = {
  // config stuff here
}

if (!firebase.apps.length) {
firebase.initializeApp(config);
}

// grab doc id from request
var id = request.query.docId;

// declare connection to firestore
var db = firebase.firestore();

// grab user from request
var docRef = db.collection("clients").doc(id);

// grab the users server and id
docRef.get().then(function(doc) {

// grab the account id
var accountId = doc.data().accountId;

// declare master variable that will be returned
var toReturn = [];

// declare variables that will be returned in toReturn
var gpmGames = [];
var cspmGames = [];
var damageGames = [];
var damageToChampionsGames = [];
var damageTakenGames = [];
var wardsGames = [];

db.collection('games')
  .where('accountId', '==', accountId)
  .get()
  .then((res) => {
    var games = res.docs;
    // iterate through games and get averages and totals to return
    games.forEach(function(game) {

      gpmGames.push(game.data().gpm);
      cspmGames.push(game.data().cspm);
      damageGames.push(game.data().damage);
      damageToChampionsGames.push(game.data().damagetochampions);
      damageTakenGames.push(game.data().damagerecieved);
      wardsGames.push(game.data().wards);

    });
  });

  toReturn['gpmGames'] = gpmGames;
  toReturn['cspmGames'] = cspmGames;
  toReturn['damageGames'] = damageGames;
  toReturn['damageToChampionsGames'] = damageToChampionsGames;
  toReturn['damageTakenGames'] = damageTakenGames;
  toReturn['wardsGames'] = wardsGames;

response.status(200).send(toReturn);
});

所以我知道在一切运行完成之前我的 return 被调用了。我该如何解决?当然,我的返回是一个空数组,我从 firebase 得到错误:错误:函数崩溃超出请求范围函数调用被中断。

谢谢!

【问题讨论】:

  • 我在想这可能是一个承诺问题?也许创建一个辅助函数?
  • 我没有看到 response.end() 是不是需要?
  • Ummmm.. 我想它可能会。我认为可以使用它来代替 response.send 如果没有任何东西要发回。在这方面我错了吗?需要它吗?

标签: javascript firebase google-cloud-functions


【解决方案1】:

你必须使用从这里返回的承诺:

db.collection('games')
  .where('accountId', '==', accountId)
  .get()

这是一个异步调用,它会立即返回一个承诺,该承诺会在工作完成后解决。

您需要使用该承诺在正确的时间发送响应:

const proimise = db.collection('games')
  .where('accountId', '==', accountId)
  .get()

promise.then(snapshot => {
    // work with the document snapshot here, and send your response
})
.catch(error => {
    // deal with any errors if necessary.
    response.status(500).send(error)
})

您发送响应的方式似乎没问题,您只需要等到提取完成即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2018-01-16
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多