Node.JS MongoDB 查找功能未运行

问题描述 投票:0回答:1

我正在尝试使用 Node.JS 从 MongoDB 集合中查找数据。然而,问题是它根本不起作用,没有错误,也没有不正确的结果。就是什么都没发生。

const mongo = require("mongodb").MongoClient
const mongoURL = "mongodb://127.0.0.1:27017"

module.exports = function (app) {
    app.route("/fetchplayer").get(fetchPlayer)
}

async function fetchPlayer(request, response) {
    const mongoClient = await mongo.connect(mongoURL)
    const project = request.query.project
    const id = request.query.id

    const playersDB = mongoClient.db("RopePlayers")
    const playersCollection = playersDB.collection(project)

    const query = { "player" : id}

    playersCollection.find(query).toArray(function (error, result) {
        if (error) throw error

        console.log(result)
    })

}

我已记录所有变量,以确保它们正常工作并且具有正确的值。我的集合当前为空,但是根据 MongoDB 文档,我仍然应该收到一个空数组。

我引用了多个文档来源,包括 W3SchoolsMonogoDB 文档以及 StackOverflow 上的文档,但没有成功。

node.js mongodb find
1个回答
0
投票

更改您的代码,如下所示:

const res = await playersCollection.find(query).toArray();

response.json(res);
© www.soinside.com 2019 - 2024. All rights reserved.