2 回答

TA貢獻1794條經驗 獲得超8個贊
我通過做我上面所做的一切來解決這個問題,并為云功能使用以下代碼
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.onFollowCreate = functions.firestore
.document("following/{userID}/myFollowing/{id}")
.onCreate((snap, context) => {
const newValue = snap.data()
const db = admin.firestore();
db.collection("users").doc(context.params.userID).update({following: admin.firestore.FieldValue.increment(1)}).catch((er)=>{console.log(er)})
db.collection('followers').doc(newValue.uid).collection("myFollowers").doc(context.params.userID).set({uid: context.params.userID, timeStamp: new Date()}).catch(er=>console.log(er))
});
exports.onFollowDelete = functions.firestore
.document("following/{userID}/myFollowing/{id}")
.onDelete((snap, context)=>{
const deletedValue = snap.data()
const db = admin.firestore();
db.collection("users").doc(context.params.userID).update({following: admin.firestore.FieldValue.increment(-1)}).catch(er=>console.log(er))
db.collection('followers').doc(deletedValue.uid).collection("myFollowers").doc(context.params.userID).delete().catch(er=>console.log(er))
})
exports.onFollowersCreate = functions.firestore
.document("followers/{userID}/myFollowers/{id}")
.onCreate((snap, context)=>{
const db = admin.firestore();
db.collection("users").doc(context.params.userID).update({followers: admin.firestore.FieldValue.increment(1)}).catch(er=>console.log(er))
})
exports.onFollowersDelete = functions.firestore
.document("followers/{userID}/myFollowers/{id}")
.onDelete((snap, context)=>{
const db = admin.firestore();
db.collection("users").doc(context.params.userID).update({followers: admin.firestore.FieldValue.increment(-1)}).catch(er=>console.log(er))
})

TA貢獻1848條經驗 獲得超10個贊
我以前也想過這個,而且非常相似。我認為這可能是構建數據庫的最佳方式。這是關于一些數據庫設計的 Medium 上的文章。
現在對于函數,您需要一個在編寫關于 A 和 B 的文檔時觸發的函數。請參閱文檔以獲取onCreate
函數。您的云功能將存在于 node.js 10 無服務器環境中,并且不會連接到您的前端應用程序。這是我在已部署站點上的一些功能的真實示例。我建議不要將數據添加到前端的 Firestore。而是創建一個onCall
HTTP 函數,在此處查看有關這些函數的更多信息。
很抱歉沒有給你實際的代碼,但我發現自己做會幫助你學習。祝你好運 :)
添加回答
舉報