我有些代码有时可以运行,但有时却没有。当它没有运行时,我收到此错误:
这是我在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]始终是一个字符串,从不为空。这个问题怎么会有时发生? 没错误:
答案 0 :(得分:4)
间歇性失败可能是因为您没有为代码执行的众多异步Firebase操作返回Promise
。 Cloud Functions Guide解释说:
管理函数的生命周期以确保它是很重要的 正确解析...此外,您可以确保云功能 运行函数的实例在函数之前不会关闭 成功达到终止条件或状态。
- 通过返回JavaScript承诺来解析执行异步处理的函数
您的代码包含对remove()
,transaction()
和update()
的调用。其中每个都异步完成并返回Promise
。您需要根据需要链接或组合(请参阅Promise.all())返回的Promises
,以确保您的函数为任何正在进行的异步Firebase操作返回Promise
。
例如,要在发布的代码末尾处理对update()
的两次调用:
const prom1 = pathSettingUpGame.update({
...
});
const prom2 = pathServer.update({
...
});
return Promise.all([prom1, prom2]);