【问题标题】:Unhandled rejection from Cloud Function, but it runs sometimes来自 Cloud Function 的未处理拒绝,但有时会运行
【发布时间】:2017-09-27 04:08:10
【问题描述】:

我有一些代码有时可以工作,但有时不能。当它不运行时,我收到此错误:

这是我的 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] 始终是一个字符串并且永远不会为空。有时怎么会出现这个问题? 没有错误:

有时我会得到这个,注意第三个值中的未定义

【问题讨论】:

  • 您没有显示整个功能。我不知道这是什么函数(数据库?)或所有变量是什么(db?)。你从函数返回什么?您是否尝试过将 console.log() 语句放入其中以验证这些值是您认为的值?
  • 我想生成5张随机卡片(图片参考和标签的字符串)并将它们上传到数据库。我用更多图片更新了我的问题。我不明白为什么会这样……

标签: javascript firebase google-cloud-functions


【解决方案1】:

间歇性故障可能是由于您没有为代码执行的众多异步 Firebase 操作返回 PromiseCloud Functions Guide 解释:

管理函数的生命周期以确保它 正确解决...此外,您可以确保 Cloud Functions 运行您的函数的实例不会在您的函数之前关闭 成功达到其终止条件或状态。

  • 通过返回 JavaScript 承诺来解析执行异步处理的函数

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

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

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

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

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

还有一个Firebase video on returning Promises

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多