Reputation: 476
Im trying to access my mongoDB and find all entries that are held in there, under 'player' names. If the player name entered is already in the DB, then simply show 'use another name'. However I'm getting back [object OBJECT] and the Chrome console shows no errors. Please can someone point out what I'm doing wrong?
I'm using the find()
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);
});
});
db.js
async function getPlayers(){
return await schemas.Player.find();
}
script.js
// This will only appear if a player has won the game, it will ask to enter their name
async function enterName() {
var personName = prompt("Please enter your name", "...");
// search database for the players name that's just been entered
//var players = getPlayers();
$.get("http://localhost:9000/getPlayers", function(data) {
//console.log(data);
alert(data);
// loop through each player (data)
$.each(data, function(key, value){
// if the name is equal to person name, output.
if (value.name == personName)
{
alert("match found");
}
});
});
}
Upvotes: 1
Views: 79
Reputation: 1282
The data
you get in the AJAX response through the API is a array of objects so you need to stringify that in your alert like alert(JSON.stringify(data));
Upvotes: 4