Firebase功能-获取我的关注者uid并将我的帖子发送给他们?

时间:2018-12-27 11:20:52

标签: javascript firebase google-cloud-functions

我正在开发社交媒体应用。当用户共享某些内容时,我会使用for loop处理手机上的所有内容。

这是我的Java代码:

 <ListBox HorizontalAlignment="Left" Height="100" Margin="80,125,0,0" VerticalAlignment="Top" Width="100" Drop="ListBox_Drop" AllowDrop="True"  PreviewMouseLeftButtonDown="ListBox_MouseLeftButtonDown">
        <ListBoxItem >item1</ListBoxItem>                                                                                                          
        <ListBoxItem >item2</ListBoxItem>                                                                                                          
        <ListBoxItem >item3</ListBoxItem>                                                                                                          
        <ListBoxItem >item4</ListBoxItem>
    </ListBox>
    <ListBox HorizontalAlignment="Left" Height="100" Margin="200,125,0,0" VerticalAlignment="Top" Width="100" Drop="ListBox_Drop" AllowDrop="True"/>


ListBox dragSource;

    private void ListBox_Drop(object sender, DragEventArgs e)
    {
        ListBox item = (ListBox)sender;
        object data = e.Data.GetData(typeof(ListBoxItem));
        dragSource.Items.Remove(data);
        item.Items.Add(data);
    }

    private void ListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ListBox source = (ListBox)sender;
        dragSource = source;
        object item= GetDataFromListBox(dragSource, e.GetPosition(source));
        if (item != null)
        {
            DragDrop.DoDragDrop(source, item, DragDropEffects.Move);
        }
    }

    private static object GetDataFromListBox(ListBox source, Point point)
    {
        UIElement element = source.InputHitTest(point) as UIElement;
        if (element != null)
        {
            object data = DependencyProperty.UnsetValue;
            while (data == DependencyProperty.UnsetValue)
            {
                data = source.ItemContainerGenerator.ItemFromContainer(element);

                if (data == DependencyProperty.UnsetValue)
                {
                    element = VisualTreeHelper.GetParent(element) as UIElement;
                }

                if (element == source)
                {
                    return null;
                }
            }

            if (data != DependencyProperty.UnsetValue)
            {
                return data;
            }
        }

        return null;
    }

但是这花费了太多时间。我想让firebase函数来处理它。这是我的javascript代码,但是它不起作用。我不知道javaScript。

这是我的JavaScript代码:

DatabaseReference reffollowers =FirebaseDatabase.getInstance().getReference().child("users").child(auth.getUid()).child("followers");
            reffollowers.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if(dataSnapshot.exists()){
                        List<String> followerslist = new ArrayList<>();
                        for(DataSnapshot ds:dataSnapshot.getChildren()){
                            String a = ds.getKey();
                            followerslist.add(a);
                        }

                        DatabaseReference refpost = FirebaseDatabase.getInstance().getReference().child("posts");
                        for(int i=0;i<followerslist.size();i++){
                            refpost.child(followerslist.get(i)).child("unique_post_key").setValue("");
                        }

                        progressDialog.dismiss();


                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

你们能帮我吗?

我的firebase数据库也是这样

  const functions = require('firebase-functions');

    const admin = require('firebase-admin');
    admin.initializeApp();

    exports.sharePost = functions.https.onCall((data, context) => {

      const postkey = data.text;
      const uid = context.auth.uid;
      var followers_list [];

      var ref = admin.database().ref("users").child(uid).child("followers");
      ref.once("value", function(snapshot) {
      snapshot.forEach(function(_child){
          followers_list.push(_child.key);
      });

  });

      for(count2 = 0; count2 < takipci_listesi.lengt; count++) {
        admin.database().ref('posts/' + followers_list[count2]).set({
          postkey: ""
        });

    }
    });

当我想获取帖子时,我使用此代码传递帖子密钥

+root
    +posts
        +user_uid
            +unique_post_key:""

1 个答案:

答案 0 :(得分:0)

我用以下代码解决了我的问题:

    const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sharePost = functions.https.onCall((data, context) => {
  const postkey = data.text;
  const uid = data.uid;

  var list= [];

  var db = admin.database();

var ref = db.ref("users").child(uid).child("followers");
  ref.once('value', function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var childKey = childSnapshot.key;
    list.push(childKey);
    console.log(list[list.length-1]+" eklendi");
    console.log("list size : "+list.length);
  });
  var refsave = db.ref("posts");

  for (var i = 0; i < list.length; i++) {
    refsave.child(list[i]).set({
       postkey:"a"
    });
    console.log(list[i]+" yazıldı");
  }

});

});