javascript - 来自 Cloud Function 的未处理拒绝,但它有时会运行

标签 javascript firebase google-cloud-functions

我有一些代码有时有效,但有时却无效。当它不运行时,我得到这个错误:

enter image description here

这是我在 Javascript 中的代码:

exports.handleRequest = functions.database.ref('/Request/{usersUID}/{autoID}/{request}').onWrite(event => {
  let request = event.data.val();
  let additionalRequest = event.data.key;
  let usersUID = event.params.usersUID;
  const generatedAutoID = event.params.autoID;
  event.data.adminRef.remove();
  if (event.data.previous.exists()) {
        return;
  }
  if (!event.data.exists()) {
        return;
  }
  const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  var db = admin.database();

  var MasterAllCards = ["2_of_clubs", "2_of_spades", "2_of_diamonds", "2_of_hearts", "3_of_clubs", "3_of_spades", "3_of_diamonds", "3_of_hearts", "4_of_clubs", "4_of_spades", "4_of_diamonds", "4_of_hearts", "5_of_clubs", "5_of_spades", "5_of_diamonds", "5_of_hearts", "6_of_clubs", "6_of_spades", "6_of_diamonds", "6_of_hearts", "7_of_clubs", "7_of_spades", "7_of_diamonds", "7_of_hearts", "8_of_clubs", "8_of_spades", "8_of_diamonds", "8_of_hearts", "9_of_clubs", "9_of_spades", "9_of_diamonds", "9_of_hearts", "10_of_clubs", "10_of_spades", "10_of_diamonds", "10_of_hearts", "jack_of_clubs", "jack_of_spades", "jack_of_diamonds", "jack_of_hearts", "queen_of_clubs", "queen_of_spades", "queen_of_diamonds", "queen_of_hearts", "king_of_clubs", "king_of_spades", "king_of_diamonds", "king_of_hearts", "ace_of_clubs", "ace_of_spades", "ace_of_diamonds", "ace_of_hearts"]
  var MasterAllValues = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14]
  function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  let pathToUsersTickets = db.ref('/users/' + usersUID + '/serverSideValues/Tickets');
  var usersTickets = 0;
  pathToUsersTickets.transaction(function(current) {
        return (current || 0) - 1;
  }).then(function(ticket) {
        usersTickets = Number(ticket.snapshot.val())
  });
  if (usersTickets >= 0) {
        let pathSettingUpGame = db.ref('/Checkrequests/' + usersUID + '/' + request); 
        let pathServer = db.ref('/Checkrequests/' + usersUID + '/' + request + '/Server');
        var tags = [];
        var images = [];
        var allCards = MasterAllCards
        var allCardsTags = MasterAllValues
        var i = 0;
        while (i < 5) {
              let randomc = getRandomInt(0, 51 - i);
              images.push(allCards[randomc])
              tags.push(allCardsTags[randomc])
              allCards.splice(randomc, 1);
              allCardsTags.splice(randomc, 1);
              i++
        }
        console.log(images);
        console.log(tags[0])
        console.log(allCards);
        console.log(allCardsTags) 
        pathSettingUpGame.update({
              "mastercard": images[0],
              "highlowgametier" : 1
        })    
        pathServer.update({
              "child1image" : images[1],
              "child2image" : images[2],
              "child3image" : images[3],
              "child4image" : images[4],
              "child1tag" : tags[1],
              "child2tag" : tags[2],
              "child3tag" : tags[3],
              "child4tag" : tags[4],
        })
   }
})

但有时它确实可以毫无错误地运行。您可以查看 here 上的代码,其中 images[0] 始终是一个字符串并且永远不会为空。怎么有时会出现这个问题呢? 没有错误: enter image description here

有时我会这样,注意第三个值中的未定义: enter image description here

最佳答案

间歇性故障可能是由于您没有为您的代码执行的大量异步 Firebase 操作返回 Promise 而造成的。 Cloud Functions Guide解释:

It's important to manage the lifecycle of a function to ensure that it resolves properly... Also, you can make sure that the Cloud Functions instance running your function does not shut down before your function successfully reaches its terminating condition or state.

  • Resolve functions that perform asynchronous processing by returning a JavaScript promise

您的代码包含对 remove()transaction()update() 的调用。其中每一个都异步完成并返回一个 Promise。您需要根据需要链接或组合(参见 Promise.all())返回的 Promise,以确保您的函数为任何正在进行的异步 Firebase 操作返回 Promise

例如,要在发布的代码末尾处理对 update() 的两次调用:

const prom1 = pathSettingUpGame.update({
    ...
});

const prom2 = pathServer.update({
    ...
});

return Promise.all([prom1, prom2]);

还有一个Firebase video on returning Promises .

关于javascript - 来自 Cloud Function 的未处理拒绝,但它有时会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690369/

相关文章:

javascript - 迭代数组中未定义的方法

javascript - 我们可以使用 javascript 从母版页访问内容页元素吗?

android - 我可以在没有谷歌服务的设备上使用谷歌Firestore吗?

angular - 如何为 Firebase Cloud Functions 使用应用程序环境变量?

firebase - 通过 firebase 功能对另一台服务器进行身份验证的最佳做法是什么?

javascript - 在不知道数据类型的情况下对 url 进行 $.ajax 调用

javascript - if 语句 getElementById.src

ios - Flutter-无法从Firebase加载此个人资料图像

firebase - 如何限制 Firebase HTTP/Callable 函数请求大小?

android - Firebase 函数在 10 秒后在客户端超时