我從在線商店收到有關已創建類別的 Webhooks。以下函式在集合內創建檔案categories
exports.createProductWebhookCategory = functions
.https.onRequest(async (request, response) => {
var category = request.body.product_type;
try {
const categoryRef = admin.firestore().collection("categories").doc(`${category}`);
await categoryRef.set({
name: category,
});
response.status(200).send("Done");
} catch (error) {
console.log(error);
response.status(400).send("Error Cat");
}
});
創建類別后,我正在呼叫 API 以在 Webflow 中創建專案。通過回傳的承諾,我得到了我想要存盤在先前創建的檔案中的專案 ID
我正在嘗試使用 5 個類別(5 個 webhook),并且 5 個類別中只有 1 個或 2 個被更新。其他檔案未更新且不包含 webflowId 欄位。更新的會隨著每次測驗運行而改變。有人知道我做錯了什么嗎?
exports.onCreateCategoryCallback = functions
.runWith({ failurePolicy: true })
.firestore
.document("/categories/{categoryId}")
.onCreate(async (snapshot, context) => {
const cat = snapshot.data();
const docId = context.params.categoryId;
const categoryRef = admin.firestore().collection("categories").doc(docId);
try {
const webflow_item = await webflow.createItem({
collectionId: 'xxx',
fields: {
'name': cat.name,
'_archived': false,
'_draft': false,
},
}, { live: true });
console.log(`ItemId for Cat ${cat.name} is ${webflow_item._id}`);
const doc = await categoryRef.get();
console.log(doc.data());
const res = await categoryRef.update({
webflowId: webflow_item._id
});
console.log(`RES for ${cat.name} is: `, res);
console.log("Function complete for cat: ", cat.name);
} catch (error) {
console.log(error);
throw 'error';
}
});
更新失敗和成功的控制臺日志如下
ItemId for Cat Coffee is 620fdc8858462f33735c986
{ name: 'Coffee' }
RES for Coffee is: WriteResult {
_writeTime: Timestamp { _seconds: 1645206666, _nanoseconds: 686306000 }
}
Function complete for cat: Coffee
uj5u.com熱心網友回復:
問題很可能來自這樣一個事實,即您沒有正確管理第二個 Cloud Function 的生命周期(Firestore 觸發了一個)。
正如您將在Firebase 官方視頻系列中有關“JavaScript Promises”的三個視頻中看到的那樣,當所有異步操作完成時,您必須在后臺觸發的 Cloud Function 中回傳一個 Promise 或一個值。通過這種方式,您可以向 Cloud Function 平臺表明它可以關閉運行您的 Cloud Function 的實體,并且您還可以避免在異步操作完成之前關閉此實體。
具體來說,有時會發生 Cloud Function 在異步操作完成之前終止的情況,因為您沒有在代碼末尾回傳 Promise 或值。其他時候,Cloud Function 平臺不會立即終止 Function,異步操作可以完成。您對此行為沒有任何控制權,因此它表現為一種不穩定的行為并且難以理解/除錯。
因此,以下改編應該可以解決問題(未經測驗):
exports.onCreateCategoryCallback = functions
.runWith({ failurePolicy: true })
.firestore
.document("/categories/{categoryId}")
.onCreate(async (snapshot, context) => {
const cat = snapshot.data();
// const docId = context.params.categoryId;
// const categoryRef = admin.firestore().collection("categories").doc(docId);
// You can replace the two above lines by the following one
const categoryRef = snapshot.ref;
try {
const webflow_item = await webflow.createItem({
collectionId: 'xxx',
fields: {
'name': cat.name,
'_archived': false,
'_draft': false,
},
}, { live: true });
console.log(`ItemId for Cat ${cat.name} is ${webflow_item._id}`);
// Not sure why you have the two next lines?? At this stage doc.data() === snapshot.data()
//const doc = await categoryRef.get();
//console.log(doc.data());
const res = await categoryRef.update({
webflowId: webflow_item._id
});
console.log(`RES for ${cat.name} is: `, res);
console.log("Function complete for cat: ", cat.name);
return null; // <== Here return a value when all the asynchronous operations complete
} catch (error) {
console.log(error);
return null;
}
});
根據上面的評論,在創建 Category 檔案時使用 Transaction 的代碼如下(同樣我沒有測驗它):
exports.createProductWebhookCategory = functions
.https.onRequest(async (request, response) => {
var category = request.body.product_type;
try {
const categoryRef = admin.firestore().collection("categories").doc(`${category}`);
await admin.firestore().runTransaction((transaction) => {
return transaction.get(categoryRef).then((doc) => {
if (!doc.exists) {
transaction.set(categoryRef, {
name: category,
})
}
});
})
response.status(200).send("Done");
} catch (error) {
console.log(error);
response.status(400).send("Error Cat");
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/428767.html
標籤:javascript 火力基地 谷歌云火库 谷歌云功能 网络流
上一篇:如果查詢知道他的兩個欄位,則查找檔案ID,firestore規則
下一篇:創建用戶之前的電子郵件驗證
