簡體   English   中英

如何在Firebase中以隨機播放模式排序帖子,而無需重復?

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

我想顯示一個來自用戶的帖子列表。 我可以按默認順序顯示它,但我想按隨機順序顯示它。 我使用的是稱為shuffle的函數,該函數可以正常工作,但是在我的代碼中無法正常工作,因為重復了很多帖子。 我的代碼是這樣的:

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

我正在使用隨機播放功能,但它重復了帖子。 你能幫我嗎? 如何按隨機順序顯示此帖子?

很難說,因為我無法運行它,並且您從未在發布的代碼中實際調用shuffle ,但在我看來,您只需要在正確的位置調用shuffle。

上線之前

postdatakey.forEach(function(postdata){

shuffle(postdatakey);

隨機排列帖子的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM