gpt4 book ai didi

mongodb - MongoError : connection destroyed, 无法实例化游标

转载 作者:可可西里 更新时间:2023-11-01 09:54:04 34 4
gpt4 key购买 nike

在 Heroku 上运行应用程序时,将 Parse-Server 与 mLab 结合使用,我在日志中收到以下错误:

app[web.1]: /app/node_modules/parse-server/lib/ParseServer.js:481
app[web.1]: throw err;
app[web.1]: ^
app[web.1]: MongoError: connection destroyed, not possible to instantiate cursor
app[web.1]: at nextFunction (/app/node_modules/mongodb-core/lib/cursor.js:607:55)
app[web.1]: at Cursor.next [as _next] (/app/node_modules/mongodb-core/lib/cursor.js:692:3)
app[web.1]: at fetchDocs (/app/node_modules/mongodb/lib/cursor.js:856:10)

让事情变得更加复杂的是,当运行相同的代码时,我并没有一直得到它。

在网络上搜索后,我了解到在尝试从数据库中读取超过 100 条记录时会发生这种情况。这似乎与我观察到的一致。

不幸的是,我没有在网上找到任何解决问题的办法。因此我的问题是:我该如何处理这个问题?

有没有办法将阈值从 100 提高到更高的值?或者有什么其他好的方法来处理这个问题?

如果这在这里可能有用,请使用执行该工作的函数的代码。在大多数情况下,它就像魅力一样。当提供的typKey恰好匹配了太多的记录时,我就遇到了上面提到的问题。

function get_List(displayPage, db, response) {
db.collection('TheCollection', function (err, collection) {
collection.find({"typeKey": "TYPE01"}).toArray(function(err, items) {
if(err) throw err;

response.render('pages/displayList.ejs', {
dataArray: items
});
});
});
}

最佳答案

您需要确保在游标完全清空后调用您的 close()。您可以通过确保使用 async/await 或 promises、.then() 来做到这一点,以便在关闭 mongo 集合之前响应有机会被完全写出

延迟以确保游标已完全完成,这种做法非常粗鲁,可能会给您带来奇怪的错误。

如果您有权访问 async/await,请将 get_list() 函数设置为异步,并确保在关闭连接之前等待它的响应。否则,将其包装在一个 Promise 中,并且仅在 promise 被解决时关闭连接。例如

function get_List(displayPage, db, response) {
db.collection('TheCollection', function (err, collection) {
collection.find({"typeKey": "TYPE01"}).toArray(function(err, items) {
if(err) throw err;

response.render('pages/displayList.ejs', {
dataArray: items
});
});
});
}

async function whichCallsget_List(request response) => {
try {
var client = await MongoClient.connect(
mongourl,
{ useNewUrlParser: true }
)
const db = await client.db(process.env.DB_NAME)

await get_List('displayPage', db, response)
db.close();
} catch (reason) {
// error handling
}
}

关于mongodb - MongoError : connection destroyed, 无法实例化游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45658647/

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