简体   繁体   English

如何在Firebase中以随机播放模式排序帖子,而无需重复?

[英]How can I sort posts in an shuffle mode in firebase, without repeat?

I want to show a list of the posts from a user following list. 我想显示一个来自用户的帖子列表。 I can show it in the default order, but I want to show it in a shuffle order. 我可以按默认顺序显示它,但我想按随机顺序显示它。 I was using a function called shuffle, that works, but in my code doesn't work correctly, because repeat many posts. 我使用的是称为shuffle的函数,该函数可以正常工作,但是在我的代码中无法正常工作,因为重复了很多帖子。 My code is this: 我的代码是这样的:

function feed(){
var postList = document.getElementById('postList');
var userId = "a";

let rangeNumbers=[];

var keys = firebase.database().ref('users/'+userId).child("following").once('value').then(function(datakey){

        let usersPost = [];
    let usersPostProfile = [];
        var contador = 0;
    let htmlPost = "";
        var i = 0;

        datakey.forEach(function(data){

            let userDB = data.val();
            let userIdFollowing = userDB.id;

            firebase.database().ref('posts/').orderByChild("id").equalTo(userIdFollowing).once('value').then(function(postdatakey){

              let cantidad = postdatakey.numChildren();


         postdatakey.forEach(function(postdata){

            //Detecta todos los datos de la publicacion
            let postDB = postdata.val();
            let postId = postDB.id;



      firebase.database().ref('/users/' + postId).once('value').then(function(snapshot) {
  let username = (snapshot.val() && snapshot.val().username);
  let name = (snapshot.val() && snapshot.val().name);
  let image = (snapshot.val() && snapshot.val().image);

  // ...
let newArray = {
            text: postDB.text,
            image: postDB.image,
            imageProfile: image,
            username: username,
            name: name,
            timestamp: postDB.timestamp
      };

 usersPost.push(newArray);

 htmlPost += 
      '<div class="postItem">'
      +'<br>'
    +'<img class="post-img-profile" src="'+usersPost[i].imageProfile+'">'
    +'<div class="userData">'

      +'<a><b>'+usersPost[i].name+'</b></a><br>'
      +'<a>'+usersPost[i].username+'</a>'
      +'</div>'
      +'<br><br><Br><br>'
      +'<div class="post">'
      +'<p>'+usersPost[i].text+'</p>'

      +'<div class="center-content">'
        +'<img class="imagePostBig" src="'+usersPost[i].image+'">'
      +'</div>'

    +'</div>'

    +'<div class="optionPost">'
      +'<img class="post-icon" src="https://img.icons8.com/ios/50/000000/like.png">'
    +'<img class="post-icon" src="https://img.icons8.com/ios/50/000000/comments.png">'
    +'<div class="line"></div>'
    +'</div>'



  +'</div>';

        //console.log(htmlPost);

        postList.innerHTML = htmlPost;

i++
      });


         });



            });  



                            });



    });  



}

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}



feed();

I was using the shuffle function but it repeat the posts. 我正在使用随机播放功能,但它重复了帖子。 Can you help me please? 你能帮我吗? How can I show this post in a shuffle order? 如何按随机顺序显示此帖子?

It's hard to tell since I can't run it and you never actually call shuffle in the code you posted but it looks to me like you just need to call shuffle in the right place. 很难说,因为我无法运行它,并且您从未在发布的代码中实际调用shuffle ,但在我看来,您只需要在正确的位置调用shuffle。

On the line before 上线之前

postdatakey.forEach(function(postdata){

add

shuffle(postdatakey);

to randomize the order of the posts. 随机排列帖子的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM