更新CloudFunctions上的子集合 [英] Update Sub-collections on CloudFunctions
问题描述
我需要某些帮助.
我的项目中有2个馆藏,一个叫做产品",另一个叫做用户".
I have 2 collections on my project, one is called Products and another called Users.
在这种情况下,我会将所选的产品信息复制到用户的子集合中作为收藏夹:
I’m duplicating the selected product information into a sub-collection of users as favorites, in this case:
用户(收藏)>用户(文档)>收藏夹(收藏)>产品(文档)
users(collection) > user(doc) > favorites(collection) > product (doc)
我的想法是创建一个云功能,该功能将监视产品集合"产品(集合)>产品(文档)中任何产品的任何更新,然后将这些更改反映在用户>收藏夹"子集合上在此处更新产品的价值:
My idea is to create a cloud-function that will watch for any updates on any product in the "product collection" product(collection) > product(doc) and then reflect those changes on the users > favorites sub-collection by updating the values of the products here:
用户(收藏)>用户(文档)>收藏夹(收藏)>产品(文档)
users(collection) > user(doc) > favorites(collection) > product (doc)
如何从index.js做到这一点?这是我的代码:
How is this possible from index.js ? This my code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.updateFavoritedProducts = functions.firestore.document('products/{productId}').onUpdate((change, context) => {
admin.firestore().collection('products').get(queryProductsSnapshot => {
queryProductsSnapshot.forEach(productQuery => {
const pricePQ = productQuery.price;
console.log('tetesfsf');
return admin.firestore().collection('users').get(queryUsersSnapshot => {
queryUsersSnapshot.forEach(userSnapshot => {
return admin.firestore().collection('users').doc(userSnapshot.id).collection('favorites').get(queryUserProductsSnapshot => {
queryUserProductsSnapshot.forEach(queryUserProductSnapshot => {
if (queryUserProductSnapshot.id === productQuery.id) {
return admin.firestore().doc(`users/${userSnapshot.id}/favorites/${queryUserProductSnapshot.id}`).update({ amountVolume: '123456'
})
} else {
console.log("No events found");
return null
}
})
});
})
})
});
})
});
推荐答案
我终于弄清楚了如何使用嵌套承诺来做到这一点.解决方法如下:
I finally figured out how to do this with nesting promises. Here is the solution:
exports.updateFavoritedProducts = functions.firestore.document('products/{productId}').onUpdate((change, context) => {
const data = change.after.data();
const productID = data.id;
return db.collection('products').where('id', '==', productID).get().then((productsQuerySnap) => {
productsQuerySnap.forEach((productQuerySnap) => {
const newPrice = productQuerySnap.data().price;
const newPriceUnit = productQuerySnap.data().priceUnit;
db.collection('users').get().then((usersQuerySnap) => {
usersQuerySnap.forEach((userQuerySnap) => {
return db.collection('users').doc(userQuerySnap.id).collection('favorites').doc(productQuerySnap.id).update({
price: newPrice,
priceUnit : newPriceUnit
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
})
.catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
return null
});
return null
})
.catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
这篇关于更新CloudFunctions上的子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!