javascript - 如何在firebase中以随机模式对帖子进行排序,而不重复?
问题描述
我想显示来自用户关注列表的帖子列表。我可以按默认顺序显示它,但我想按随机顺序显示它。我正在使用一个名为 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);
使帖子的顺序随机化。
推荐阅读
- python - GUI Tkinter 中的 TypeError:ask_stableLvl() 缺少 1 个必需的位置参数:“输入”
- amazon-web-services - 使用 aws codebuild 未收到有关 bitbucket 拉取请求的 Sonar 评论
- javascript - 等待一个不返回承诺的异步方法被明确认为是一种不好的做法?
- php - php插入数据库函数然后在另一个函数错误中全选
- wcf - 我们可以为统一容器中的单例实例定义超时吗?
- sql - Sequelize -> Group By field 并选择另一个字段的最大值
- jquery - jQuery Document Ready 有时不执行
- performance - 如何在注册表单中捕获电子邮件验证时间?
- html - 使用 CSS 选择器
- rest - 运行 docker 容器时,eureka 无法注册客户端应用程序,但在本地,系统按预期工作