如何在Firebase中创建函数? [英] How to create functions in firebase?

查看:44
本文介绍了如何在Firebase中创建函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firebase中使用计数器功能.该功能的作用是为有多少人喜欢该视频添加计数器,并为这些人所投票的计数添加计数器.举个例子.使用者A给予3星(5星)来喜欢影片.然后,该函数添加到like plus1和用户评级字段plus3中,然后我还具有此delete函数,其计算负3星和负1like.我现在想要的是改善功能,同时还为一个用户计算他上传了多少个视频以及他产生了多少颗星的计数器.例如,某用户上传了5个视频,而他们有20个喜欢的视频,总共获得了30颗星,然后,当一个用户不喜欢自己的视频时,又将其减去.

这里有我的功能:

  exports.onLikeCreated = functions.firestore.document("videos/{videoUid}/uservotes/{voteUid}").onCreate((snap,context)=> {const db = admin.firestore();const {videoUid} = context.params;const {rating = 0} = snap.data();constcrementLikes = admin.firestore.FieldValue.increment(1);const gainRating = admin.firestore.FieldValue.increment(rating);返回数据库.doc(`videos/$ {videoUid}`).set({likes:crementLikes,rating:incrementRating},{merge:true});});exports.onLikeDeleted = functions.firestore.document("videos/{videoUid}/uservotes/{voteUid}").onDelete((snap,context)=> {const db = admin.firestore();const {videoUid} = context.params;const {rating = 0} = snap.data();constcrementLikes = admin.firestore.FieldValue.increment(-1);constcrementingRating = admin.firestore.FieldValue.increment(rating * -1);返回数据库.doc(`videos/$ {videoUid}`).set({likes:crementLike,rating:incrementRating},{merge:true});}); 

因此,我的功能再次正常运行.我现在想要的是添加2个功能作为新功能,或者如果它也能正常工作,则只需使用2个计数器更新我已经存在的功能,就可以对来自同一用户的每个视频的第一个计数器进行更新.另一个用于计算(同一用户的)所有视频的评分.但是,如果有人不喜欢它,那么它还应该更新计算的收视率值和视频计数器,如果videoiwner删除了一个视频.我还应该说,当用户喜欢时,他给了3星,那就是评级.我需要一起计算的收视率之类的.例如,第一个用户喜欢给3星的帖子.然后,喜欢计数器应为1,评分应为3.然后第二个用户喜欢相同的帖子并给予5颗星.像计数器应更新为2并评级为8.该功能应与我已经使用的功能完全相同.只是不要为singel视频创建类似的计数器和评分计数器.为同一用户的所有视频创建一个喜欢的计数器和评分计数器.

我想将新的2个计数器或功能保存在此路径"meinprofilsettings/{uid}"中.已经存在用户数据,这将使访问变得很容易,如果不可能的话,则可能是在该数据的一个子选项中.数据库的外观如下:

此处显示我的视频收藏集的外观

这是它的外观

解决方案

在我看来,您想跟踪用户从所有视频中获得的所有总喜欢和总评分.您可以在meinprofilsettings中增加该值,并在视频获得点赞并减少点赞减少后继续增加该值.

 exports.onLikeCreated = functions.firestore.document("videos/{videoUid}/uservotes/{voteUid}").onCreate(async(snap,context)=> {const db = admin.firestore();const {videoUid} = context.params;const {rating = 0} = snap.data();constcrementLikes = admin.firestore.FieldValue.increment(1);constcrementRating = admin.firestore.FieldValue.increment(rating);const userId =(等待db.doc(`videos/$ {videoUid}`).get()).data().userId返回Promise.all([db.doc(`videos/$ {videoUid}`).set({喜欢:incrementLikes,评分:incrementRating},{merge:true}),db.doc(`meinprofilsettings/$ {userId}`).update({totalLikes:crementLikes,totlaRating:incrementRating})])}); 

您需要知道上传视频的用户ID,所以我已经在那获取了用户文档,然后在两个地方都增加了值.

要减小值时,请遵循相同的步骤:

 exports.onLikeDeleted = functions.firestore.document("videos/{videoUid}/uservotes/{voteUid}").onDelete((snap,context)=> {const db = admin.firestore();const {videoUid} = context.params;const {rating = 0} = snap.data();const decrementLikes = admin.firestore.FieldValue.increment(-1);const decrementRating = admin.firestore.FieldValue.increment(rating * -1);const userId =(等待db.doc(`videos/$ {videoUid}`).get()).data().userId返回Promise.all([db.doc(`videos/$ {videoUid}`).set({likes:decrementLike,rating:decrementRating},{merge:true}),db.doc(`meinprofilsettings/$ {userId}`).update({totalLikes:decrementLikes,totlaRating:decrementRating})])}); 

让我知道是否还有其他查询.

Im using a counter function in firebase. What the function does is add counter for how many people liked that video and also a counter for how many votes these people giving. So for example . User A liked the video with giving 3 of 5 stars. Then the function is adding to the like plus1 and to the user rating field plus3 and then I have also this delete function where its calculating minus 3 stars and minus 1like. What I want now is improve the function with also calculating for one user a counter of how many videos he uploaded and how many stars he generated . For example user a uploads 5 videos and they have 20 likes with a total of 30 stars and then also when one user dislike his video again calculate it minus.

Heres my functions:

exports.onLikeCreated = functions.firestore
    .document("videos/{videoUid}/uservotes/{voteUid}")
    .onCreate((snap, context) => {
      const db = admin.firestore();
      const {videoUid} = context.params;
      const {rating = 0} = snap.data();
      const incrementLikes = admin.firestore.FieldValue.increment(1);
      const incrementRating = admin.firestore.FieldValue.increment(rating);
      return db
          .doc(`videos/${videoUid}`)
          .set({likes: incrementLikes, rating: incrementRating}, {merge: true});
    });
exports.onLikeDeleted = functions.firestore
    .document("videos/{videoUid}/uservotes/{voteUid}")
    .onDelete((snap, context) => {
      const db = admin.firestore();
      const {videoUid} = context.params;
      const {rating = 0} = snap.data();
      const incrementLikes = admin.firestore.FieldValue.increment(-1);
      const incrementRating = admin.firestore.FieldValue.increment(rating * -1);
      return db
          .doc(`videos/${videoUid}`)
          .set({likes: incrementLikes, rating: incrementRating}, {merge: true});
    });

So again my functions working perfectly . What i want now is adding 2 fcuntions as new funtions or if its also works just update my already existing functions with 2 counters the first for every like of every video from the same user . And the other for the rating of all videos (of the same user )together calculated. But if somebody disliked it, then it should also update the calculated rating value and the video counter if videoiwner deleted one video. Also i should say When a user liked then he gives for example 3 stars and thats the rating . I need the ratings calculated together and also the like . For example first user liked post with giving 3 stars. Then like counter should be 1 and rating should be 3. Then second user liked the same post and giving 5 stars. like counter should be updated to 2 and rating to 8 . The functionality should be excatly the same as the funtions that i already use .Just instead of creating a like counter and rating counter for a singel video. Creating a like counter and rating counter of all videos of the same user .

I wanna save the new 2 counters or funtions inside this path "meinprofilsettings/{uid}". There already exist user data that will make it easy to get access , if its not possible then maybe in a subcoleltion of that Heres how the database looks

Heres how my videos collection looks

This is how it looks

解决方案

It seems to me you want to keep track of all total likes and total rating collectively gained by a user from all the videos. You can increment that value in the meinprofilsettings and keep incrementing it when a video gets a like and and decrement by the like is removed.


exports.onLikeCreated = functions.firestore
    .document("videos/{videoUid}/uservotes/{voteUid}")
    .onCreate(async (snap, context) => {
        const db = admin.firestore();
        const { videoUid } = context.params;
        const { rating = 0 } = snap.data();
        const incrementLikes = admin.firestore.FieldValue.increment(1);
        const incrementRating = admin.firestore.FieldValue.increment(rating);
        const userId = (await db.doc(`videos/${videoUid}`).get()).data().userId
        return Promise.all([
            db.doc(`videos/${videoUid}`).set({ likes: incrementLikes, rating: incrementRating }, { merge: true }),
            db.doc(`meinprofilsettings/${userId}`).update({ totalLikes: incrementLikes, totlaRating: incrementRating })
        ])
    });

You'll need to know the user Id who has uploaded the video so I have fetched the user document there and then increment the value at both places.

When it comes to decrementing the values, follow the same:


exports.onLikeDeleted = functions.firestore
    .document("videos/{videoUid}/uservotes/{voteUid}")
    .onDelete((snap, context) => {
        const db = admin.firestore();
        const { videoUid } = context.params;
        const { rating = 0 } = snap.data();
        const decrementLikes = admin.firestore.FieldValue.increment(-1);
        const decrementRating = admin.firestore.FieldValue.increment(rating * -1);
        const userId = (await db.doc(`videos/${videoUid}`).get()).data().userId
        return Promise.all([
            db.doc(`videos/${videoUid}`).set({ likes: decrementLikes, rating: decrementRating }, { merge: true }),
            db.doc(`meinprofilsettings/${userId}`).update({ totalLikes: decrementLikes, totlaRating: decrementRating })
        ])
    });

Let me know if there are any further queries.

这篇关于如何在Firebase中创建函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆