如果某個欄位中的值低于某個數字,我一直在嘗試為自己添加自動電子郵件功能。我的資料庫看起來像這個firestore 資料庫
我想撰寫一個云函式,自動將資料添加到郵件集合,然后使用 Firebase Trigger Email 觸發電子郵件。我正在尋找的觸發器是 price_change(它是字串格式,因此我試圖在云函式中將其轉換為 int)并比較它并更新郵件集合。
我的代碼看起來像這樣。我如何解決它?
exports.btcMail = functions.firestore
.document('cryptos/Bitcoin/1h/{wildcard}')
.onWrite((change, context) => {
const newval = change.data();
console.log(newval)
const price = ParsInt(newval.price_change())
if (price < -200) {
admin.firestore().collection('mail').add({
to: '[email protected]',
message: {
subject: 'big change in bitcoin prices!',
html: 'This is an <code>HTML</code> email body.',
},
});}
});
uj5u.com熱心網友回復:
由于向 Firestore 添加檔案是一個異步操作,您需要從該操作回傳承諾。否則,Cloud Functions 可能會在您有機會撰寫檔案之前終止您的代碼。
exports.btcMail = functions.firestore
.document('cryptos/Bitcoin/1h/{wildcard}')
.onWrite((change, context) => {
const newval = change.data();
console.log(newval)
const price = ParsInt(newval.price_change())
if (price < -200) {
return admin.firestore().collection('mail').add({ // ?? add return here
to: '[email protected]',
message: {
subject: 'big change in bitcoin prices!',
html: 'This is an <code>HTML</code> email body.',
},
});
} else {
return null; // ?? Add a return here to prevent paying longer than needed
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/328999.html
標籤:节点.js 火力基地 谷歌云firestore 谷歌云功能
上一篇:是否可以在Firebase存盤中將第一幀作為影像形式的視頻存盤?
下一篇:組合多個集合組查詢
