我正在嘗試執行一些實時資料庫任務,完成后應該將結果發送回客戶端,以便客戶端知道任務何時完成。
exports.createGame = functions.https.onCall((data, context) => {
const gameChosen = data.gameChosen;
const adminName = data.adminName;
const numberOfRounds = data.numberOfRounds;
let gameID = Math.floor(100000 Math.random() * 900000).toString();
var numberOfQuestions = 0;
var questionsPicked = [];
getGameQuestions(gameChosen, numberOfRounds).then((questionsPickedArray) => {
db.ref(`live-games/${gameChosen}/${gameID}`).set(
{
playerAdmin: adminName,
game: gameChosen,
players: [adminName],
questions: questionsPickedArray,
datetimeCreated: Date.now(),
},
(error) => {
if (error) {
console.log("Data could not be saved." error);
} else {
console.log("Data saved successfully.");
return {
gameChosen: gameChosen,
gameAdmin: adminName,
questions: questionsPicked,
gameID: gameID,
};
}
}
);
});
});
function getGameQuestions(gameChosen, numberOfRounds) {
var questionsPicked = [];
var numberOfQuestions = 0;
return new Promise(function (resolve, reject) {
db.ref(`games/${gameChosen}`).once("value", function (snapshot) {
val = snapshot.val();
numberOfQuestions = val.questionsAvailable;
for (var i = 0; i < numberOfRounds; i ) {
let questionNumber = Math.floor(Math.random() * (numberOfQuestions - 0 1) 0);
if (!questionsPicked.includes(questionNumber)) {
questionsPicked.push(questionNumber);
}
}
resolve(questionsPicked);
});
});
}
由于某些實時資料庫任務不回傳承諾,我試圖創建一個承諾 - 不確定是哪一個。根據日志,該函式以狀態代碼完成200
,然后幾秒鐘后,實時資料庫使用這些值更新。應該更新資料庫,將結果發送回客戶端,然后函式應該完成。它當前正在將 NULL 發送回客戶端 - 假設該函式在運行后立即將其發送回。
如何高效地一個接一個地執行實時資料庫任務?
uj5u.com熱心網友回復:
以下修改應該可以解決問題:
exports.createGame = functions.https.onCall((data, context) => {
const gameChosen = data.gameChosen;
const adminName = data.adminName;
const numberOfRounds = data.numberOfRounds;
let gameID = Math.floor(100000 Math.random() * 900000).toString();
var numberOfQuestions = 0;
var questionsPicked = [];
return getGameQuestions(gameChosen, numberOfRounds) // We return the Promises chain, see below for more details
.then((questionsPickedArray) => {
return db.ref(`live-games/${gameChosen}/${gameID}`).set(
{
playerAdmin: adminName,
game: gameChosen,
players: [adminName],
questions: questionsPickedArray,
datetimeCreated: Date.now(),
})
})
.then(() => {
return {
gameChosen: gameChosen,
gameAdmin: adminName,
questions: questionsPicked,
gameID: gameID,
};
})
.catch((error) => {
// See https://firebase.google.com/docs/functions/callable#handle_errors
throw new functions.https.HttpsError(...);
});
});
function getGameQuestions(gameChosen, numberOfRounds) {
var questionsPicked = [];
var numberOfQuestions = 0;
return db.ref(`games/${gameChosen}`).once("value") // Here too, we return the Promises chain
.then(snapshot => {
val = snapshot.val();
numberOfQuestions = val.questionsAvailable;
for (var i = 0; i < numberOfRounds; i ) {
let questionNumber = Math.floor(Math.random() * (numberOfQuestions - 0 1) 0);
if (!questionsPicked.includes(questionNumber)) {
questionsPicked.push(questionNumber);
}
}
return questionsPicked;
});
}
因此,您需要鏈接 Promises并將結果回傳給客戶端,如上所示:如檔案中所述“promise 回傳的資料被發送回客戶端”(如果我們回傳 Promises 鏈就是這種情況)并且資料應該是可以進行 JSON 編碼的物件/值({ gameChosen: gameChosen, gameAdmin: adminName, ...}
物件就是這種情況)。
對于getGameQuestions()
函式,由于once()
方法回傳的是Promise,所以不需要將其封裝成Promise。同樣,只需回傳由once().then(...)
.
uj5u.com熱心網友回復:
您必須按照此處所述回傳該Promise。
要在異步操作后回傳資料,請回傳一個 Promise。
exports.createGame = functions.https.onCall((data, context) => {
const gameChosen = data.gameChosen;
const adminName = data.adminName;
const numberOfRounds = data.numberOfRounds;
let gameID = Math.floor(100000 Math.random() * 900000).toString();
var numberOfQuestions = 0;
var questionsPicked = [];
getGameQuestions(gameChosen, numberOfRounds).then((questionsPickedArray) => {
//Just add a return here
return db.ref(`live-games/${gameChosen}/${gameID}`).set(
{
playerAdmin: adminName,
game: gameChosen,
players: [adminName],
questions: questionsPickedArray,
datetimeCreated: Date.now(),
})
.catch((error) => {
if (error) {
console.log("Data could not be saved." error);
} else {
console.log("Data saved successfully.");
return {
gameChosen: gameChosen,
gameAdmin: adminName,
questions: questionsPicked,
gameID: gameID,
};
}
})
);
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492532.html