亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

制作具有firestore和云功能的跟隨系統

制作具有firestore和云功能的跟隨系統

慕村225694 2022-11-03 10:09:40
嗨,我正在嘗試在 Firestore 上制作社交媒體應用程序。現在在這里為跟隨系統建模是我的計劃。用戶(集合){uid} 文檔,其中包含關注者和關注者作為數字。關注(收藏)myFollowing(子集合){其他用戶的uid}{uid}追隨者(收藏)我的追隨者(子集合){其他用戶的uid}{uid}所以這是我的計劃,請隨時批評并幫助我做得更好,因為我不知道這是否是最好的方法。當用戶A關注用戶B時,我會寫一個文檔:下列的我的關注建造一個uid此寫入將直接從應用程序發生。之后我計劃觸發一個云函數,它會做兩件事,1. 它將增加用戶集合中的一個計數器,該計數器包含總關注量。2.它會寫另一個文件追隨者我的追隨者一個uid建造在此之后,我可以擁有另一個云功能,每當在 Followers/uid/myFollowers 集合中創建文檔時觸發,它會增加用戶集合中的關注者計數。所以這里有問題這是解決這個問題的最好方法嗎?如何編寫云函數?感謝你給與我的幫助!
查看完整描述

2 回答

?
幕布斯7119047

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))

    })


查看完整回答
反對 回復 2022-11-03
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我以前也想過這個,而且非常相似。我認為這可能是構建數據庫的最佳方式。這是關于一些數據庫設計的 Medium 上的文章。

現在對于函數,您需要一個在編寫關于 A 和 B 的文檔時觸發的函數。請參閱文檔以獲取onCreate函數。您的云功能將存在于 node.js 10 無服務器環境中,并且不會連接到您的前端應用程序。這是我在已部署站點上的一些功能的真實示例。我建議不要將數據添加到前端的 Firestore。而是創建一個onCallHTTP 函數,在此處查看有關這些函數的更多信息。

很抱歉沒有給你實際的代碼,但我發現自己做會幫助你學習。祝你好運 :)


查看完整回答
反對 回復 2022-11-03
  • 2 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號