javascript - Vue 和 Firebase 问题 : Error: function crashed out of request scope Function invocation was interrupted

标签 javascript firebase google-cloud-functions

我见过这个问题,但没有好的答案。我可以理解问题所在,但似乎不知道如何解决。这是我的功能:

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

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

谢谢!

最佳答案

您必须使用由此返回的 promise :

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

这是一个异步调用,它会立即返回,并带有一个 promise ,当工作完成时,该 promise 将被解决。

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

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

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

关于javascript - Vue 和 Firebase 问题 : Error: function crashed out of request scope Function invocation was interrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48773905/

相关文章:

firebase - firebase deploy --only 功能上出现 NPM 错误的原因是什么

带参数的 Flutter 云函数调用

javascript - 使用python或js将带有dynamic-css的html下载为pdf

javascript - 创建 Angular 指令说未定义不是函数

swift - 使用 Firebase 的 OAuth 问题通过 Facebook 登录(input_token 中的 App_id 与查看应用程序不匹配)

firebase - 我应该如何在Flutter应用中使用FirebaseAuth.instance.onAuthStateChanged?

javascript - 正确卸载 react 组件

JavaScript 在点击时生成新的输入字段

javascript - 如何查询兄弟节点的现有子节点并循环它们以检测其是否存在?

node.js - firebase 函数中的 "firebase serve"未运行最新更改