制作具有firestore和云功能的跟随系统

嗨,我正在尝试在 Firestore 上制作社交媒体应用程序。

现在在这里为跟随系统建模是我的计划。

  • 用户(集合)

    • {uid} 文档,其中包含关注者和关注者作为数字。

  • 关注(收藏)

    • myFollowing(子集合)

    • {其他用户的uid}

    • {uid}

    • 追随者(收藏)

      • 我的追随者(子集合)

      • {其他用户的uid}

      • {uid}

      所以这是我的计划,请随时批评并帮助我做得更好,因为我不知道这是否是最好的方法。

      当用户A关注用户B时,我会写一个文档:

      • 下列的

        • 我的关注

        • 建造

        • 一个uid

        此写入将直接从应用程序发生。

        之后我计划触发一个云函数,它会做两件事,1. 它将增加用户集合中的一个计数器,该计数器包含总关注量。2.它会写另一个文件

        • 追随者

          • 我的追随者

          • 一个uid

          • 建造

          在此之后,我可以拥有另一个云功能,每当在 Followers/uid/myFollowers 集合中创建文档时触发,它会增加用户集合中的关注者计数。

          所以这里有问题

          1. 这是解决这个问题的最好方法吗?

          2. 如何编写云函数?

          感谢你给与我的帮助!


          慕村225694
          浏览 85回答 2
          2回答

          幕布斯7119047

          我通过做我上面所做的一切来解决这个问题,并为云功能使用以下代码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))    })

          慕桂英546537

          我以前也想过这个,而且非常相似。我认为这可能是构建数据库的最佳方式。这是关于一些数据库设计的 Medium 上的文章。现在对于函数,您需要一个在编写关于 A 和 B 的文档时触发的函数。请参阅文档以获取onCreate函数。您的云功能将存在于 node.js 10 无服务器环境中,并且不会连接到您的前端应用程序。这是我在已部署站点上的一些功能的真实示例。我建议不要将数据添加到前端的 Firestore。而是创建一个onCallHTTP 函数,在此处查看有关这些函数的更多信息。很抱歉没有给你实际的代码,但我发现自己做会帮助你学习。祝你好运 :)
          打开App,查看更多内容
          随时随地看视频慕课网APP

          相关分类

          JavaScript