gpt4 book ai didi

javascript - 试图通过 find() 在我的 MongoDB 中查找条目,但只是返回 [object Object]

转载 作者:行者123 更新时间:2023-11-30 10:57:09 25 4
gpt4 key购买 nike

我正在尝试访问我的 mongoDB 并找到其中以“玩家”名称命名的所有条目。如果输入的玩家名称已经在数据库中,则只需显示“使用其他名称”。但是我正在返回 [object OBJECT] 并且 Chrome 控制台没有显示任何错误。请有人指出我做错了什么?

我正在使用 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");

}
});
});
}

Local Host returns this message

最佳答案

您通过 API 在 AJAX 响应中获得的 data 是一个对象数组,因此您需要在警报中对其进行字符串化,例如 alert(JSON.stringify(data));

关于javascript - 试图通过 find() 在我的 MongoDB 中查找条目,但只是返回 [object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59543152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com