云功能:避免在不同情况下嵌套承诺

时间:2020-04-11 01:13:56

标签: javascript firebase promise google-cloud-functions

我正在尝试编写一个在触发Stores/{storeId}/{departmentId}/{productId}时要考虑3个条件的函数,并在ref.child('Home').child('Chiep').child(departmentId).child(productId)中写入新数据。

1)当firestore中没有数据时,我需要通过在2个不同的Realtime DB节点中进行查询来填充firestore中的所有字段:{{1 }}和Stores来拍摄照片。

2)当在Products节点中进行更改且更改来自同一Stores时,我只需要更新一些数据而无需进行任何其他查询。

3)最后,当在{storeId}节点中进行更改并且来自其他Stores的更改时,我只需要在{storeId}节点中进行一个查询。

Stores

问题是:查询或不查询另一个exports.homeChiepest = functions.firestore .document('Stores/{storeId}/{departmentId}/{productId}') .onWrite((change, context) => { const storeId = context.params.storeId; const departmentId = context.params.departmentId; const productId = context.params.productId; const ref = admin.database().ref(); // Get an object with the current document value. // If the document does not exist, it has been deleted. const document = change.after.exists ? change.after.data() : null; // Get an object with the previous document value (for update or delete) const oldDocument = change.before.exists ? change.before.data() : null; // Prevent infinite loops if (!change.after.exists) { console.log('DATA DELETED RETURN NULL'); return null; } const newPrice = document.price; const newTimestamp = document.timestamp; return ref.child('Home').child('Chiep') .child(departmentId).child(productId) .once('value') .then(dataSnapshot => { if (dataSnapshot.val() !== null) { console.log('CHIEP DOES exist'); const oldPrice = dataSnapshot.val().price; const storeKey = dataSnapshot.val().storeKey; if (storeId === storeKey) { console.log('SAME STORE - Change price and timestamp'); var newChiepest = { timestamp: newTimestamp, price: newPrice }; return dataSnapshot.ref.update(newChiepest); } else { console.log('OTHER STORE - Verify if price is chieper...'); if (newPrice <= oldPrice) { console.log('NEW PRICE: '+newPrice+' is chieper than the older one: '+oldPrice); return change.after.ref.parent.parent.get().then(doc => { // HERE Avoid nesting promises newStoreImg = doc.data().image; var newStoreChiep = { price: newPrice, storeImg: newStoreImg, storeKey: storeId, timestamp: newTimestamp }; return dataSnapshot.ref.update(newStoreChiep); }); } else { console.log('NEW PRICE: '+newPrice+' is mode EXPENSIVE than the older one: '+oldPrice); } return null; } } else { console.log('data does NOT exist, so WRITE IT!'); let getStoreData = change.after.ref.parent.parent.get(); let getProductData = admin.firestore().collection('Products').doc('Departments').collection(departmentId).doc(productId).get(); return Promise.all([getStoreData, getProductData]).then(values => { // HERE Avoid nesting promises const [store, product] = values; var newHomeChiepest = { depId: departmentId, price: newPrice, prodImg: product.data().image, prodKey: productId, storeKey: storeId, storeImg: store.data().image, timestamp: newTimestamp }; return dataSnapshot.ref.set(newHomeChiepest); }); } }) .catch(error => { console.log('Catch error reading Home: ',departmentId ,'/', productId,'; message: ',error); return false; }); }); 节点的不同可能性使我在上载firestore时出现警告,即:

警告避免嵌套承诺诺言/不嵌套

感谢您对重构此代码的任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用变量来管理“分流”,具体取决于不同的情况,如下所示(未测试):

toString()