0

I am using MongoDB and Node.JS, I am trying to get data out of my MongoDB and show into my html page which I have working with the below code however this just brings back ALL entries in no particular order:

server.js

// This is for getting the list of all players from my DB
app.get("/getPlayers", function(request, response) {
    db.getPlayers().then(function(players){
        console.log(players);
        response.send(players);
    });
});

leadership.html

<script>
    $(function() {
        $.get("http://localhost:9000/getPlayers", {}, function (res) {
        let data = res;
        console.log(res);

        for (i = 0; i < data.length; i++) {
            let name = data[i].name;
            let score = data[i].score;

            console.log(data[i].name);


            $("#leadership").append("<tr><td class=\"name\">"
                + data[i].name + "</td><td class=\"score\">" 
                + data[i].score + "</td></tr>");
         }
     });
});
</script>

After looking at W3 Schools I tried to alter the code to this:

db.getPlayers().sort().limit(10).then(function(players)

However my Chrome console brings back an internal server error 500. Can someone point out how I can sort by LARGEST NUMBER first, then LIMIT the results to say 10? Within the database there is a collection called players, which holds name and score

db.js

var Player = mongoose.model("Player", {name: String, score: Number}); 

    module.exports.Player = Player;
14
  • What field are you sorting on? Commented Jan 1, 2020 at 14:46
  • "my Chrome console brings back an internal server error 500" - what does your server log say? Commented Jan 1, 2020 at 14:47
  • @SergioTulentsev It says Reference error: score is not defined Commented Jan 1, 2020 at 14:50
  • @prasad_ I need to sort on the score field and then have highest at the top, and then limit those results to say 10? Commented Jan 1, 2020 at 14:51
  • See this example. Commented Jan 1, 2020 at 14:52

1 Answer 1

2

Try something like this. Order, sort and limit can be passed from front end or change default values after : mark.

Players is imported model, you can do it this way or use method in the model itself.

app.post('/api/players',(req,res)=>{

    let order = req.body.order ? req.body.order : "-1";
    let sortBy = req.body.sortBy ? req.body.sortBy : "_id";
    let limit = req.body.limit ? parseInt(req.body.limit) : 100;      

    Players.
    find().
    sort([[sortBy,order]]).
    limit(limit).
    exec((err,players)=>{
        if(err) return res.status(400).send(err);
        res.status(200).json({
            size: players.length,
            players
        })
    })
})

Sign up to request clarification or add additional context in comments.

1 Comment

Can be passed, but shouldn't (not in this unchecked manner, at least). With this, you yourself are opening the app for a DoS attack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.