HK420
October 21, 2020, 12:58pm
1
Ok, so my code is:
<ul>
<tbody id="myTable">
<% data.forEach(function(BoostData) { %>
<a href="/info/<%= BoostData.guildID %>"><strong><%= BoostData.guildName %></strong></a>
<p>Boosts: <b><%= BoostData.boostAmount %></b>
<% if(BoostData.serverDescription === "None") { %>
<% } else {%>
<p class="centerServerDesc"><%= BoostData.serverDescription %></p>
<% } %>
<% if(BoostData.serverInvite === "None") { %>
<p><b>No server invite</b></p>
<% } else { %>
<a href="https://<%= BoostData.serverInvite %>">
<p><b>Server Invite</b></p>
<% } %>
<% if(BoostData.guildIcon === `https://cdn.discordapp.com/icons/${BoostData.guildID}/null`) { %>
<img class="serverIcon"
src="https://images-ext-2.discordapp.net/external/ouGhEoGzz1ZyBG9mMFrYClvdv9V0FZ0jGSEHa_kLLYk/https/discordapp.com/assets/0e291f67c9274a1abdddeb3fd919cbaa.png">
<% } else { %>
<img class="serverIcon" src="<%= BoostData.guildIcon %>">
<% } %>
<a href="/info/<%= BoostData.guildID %>" class="viewServer">View Server</a>
<br><br><br>
<% }); %>
</tbody>
</ul>
And I want to sort everything by the BoostData.boostAmount
. The highest server that’s boosted will be at the top. How can I do this? Im getting all my data from mongodb and it’s not in an array.
Probably you should sort on server side like,
let sorted = data.sort((a, b) => b.boostAmount - a.boostAmount);
// note that I reversed the order of var a, b inside fn, to sort in DESC.
Search for “sort array in js” if you want details
HK420
October 21, 2020, 1:17pm
3
Sadly, my data isn’t in an array.
Then how are you executing forEach method, if it’s not an array?
1 Like
HK420
October 21, 2020, 1:21pm
5
Lemme show you some of the server side code.
let boostdata = await Boost.find({
lb: "all"
}, (err, data) => {
if (err) console.log(err)
})
res.render('pages/leaderboard', {
data: boostdata,
})
The model:
const mongoose = require('mongoose');
const boostSchema = mongoose.Schema({
guildID: String,
guildName: String,
boostAmount: Number,
cooldown: Number,
lb: {
type: String,
default: "all"
},
guildIcon: String,
serverInvite: String,
serverDescription: String,
imageOne: String,
imageTwo: String,
imageThree: String
})
module.exports = mongoose.model("Boosts", boostSchema)
@HK420 - Can you check log of typeof boostdata
and "length" in data
just before res.render()
I guess it should be array, if you can do forEach…
HK420
October 21, 2020, 1:41pm
7
The length is 0 rn because I don’t have any data in my mongodb rn.
HK420
October 21, 2020, 1:42pm
8
What the forEach is doing is its going through all the data for each guild and displaying it…
Also, it is an array but inside the array is json data.
HK420
October 21, 2020, 2:52pm
9
Hmmm,
I tried doing:
boostdata.forEach(function(BoostData) {
boostDataArr = []
boostDataArr.push(BoostData.boostAmount)
console.log(boostDataArr)
})
And I added data from two servers but now im getting:
(Each server has one boost, that’s why both values are 1.)
UPDATE:
Just fixed it by doing:
boostDataArr = []
boostdata.forEach(function(BoostData) {
boostDataArr.push(BoostData.boostAmount)
})
console.log(boostDataArr)
1 Like
HK420
October 21, 2020, 3:03pm
10
@vrintle
var boostDataArr = []
boostdata.forEach(function(BoostData) {
boostDataArr.push(BoostData.boostAmount)
})
let test = await boostDataArr.sort(function(a, b){return b - a})
console.log(test)
I have this on my server side. Now, how would I add it to my ejs?
vrintle
October 21, 2020, 3:10pm
11
@HK420 - I would suggest you to add BoostData
as whole, as I guess you’ll render other props on frontend. Then, you have to sort like I did before.
I think it would be same as,
res.render('pages/leaderboard', {
data: test,
})
HK420
October 21, 2020, 3:44pm
12
@vrintle what would I write in my ejs file?
I was never very good at reading mongoose schemas - can you provide a sample of the data stored in your database?
In <% data.forEach...
you have to reverse the data
, so just add .reverse()
after data
HK420
October 22, 2020, 12:20pm
17
Well… it reversed the data but I want to order the boostAmount from greatest to least.
HK420
October 22, 2020, 12:21pm
18
Welp, I figured out how to do it…
<% data.sort((a, b) => b.boostAmount - a.boostAmount).forEach(function(BoostData) { %>
Added this in the foreach.
2 Likes
system
Closed
April 20, 2021, 12:21pm
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.