MongoError: Cursor Not found, cursor id xxxxx
API代码如下:
app.get('/lowest', async (req, res, next) => {
let url = 'mongodb://url';
let param1 = req.query.param1;
let param2 = req.query.param2;
let client = mongodb.MongoClient;
try {
client.connect(url, function (err, client) {
let db = client.db("db1");
let collection = db.collection('collection1');
let options = {
allowDiskUse: false,
timeout: false,
noCursorTimeout: true
};
// returns mongodb aggregation pipeline
let pipeline = helper.getLowestPipeline(param1, param2);
//Step 1: declare promise
let myPromise = () => {
return new Promise((resolve, reject) => {
collection.aggregate(pipeline, options).toArray(function (err, data) {
err
? reject(err)
: resolve(data);
});
});
};
//Step 2: async promise handler
let callMyPromise = async () => {
let result = await (myPromise());
client.close();
//anything here is executed after result is resolved
return result;
};
//Step 3: make the call
callMyPromise().then(function (result) {
res.send(JSON.stringify({"status": 200, "error": null, "response": result}));
});
});
} catch (e) {
client.close();
res.json({"status": 500, "error": e, "response": null});
res.end();
}
});
pm2监控结果如下:
我无法确定问题所在,任何建议都将是有帮助的...MongoError: 游标未找到,游标ID:7820213409290816,命名空间:db_name.collection_name
在 Connection. (/rc19_pricemongo/node_modules/mongodb/lib/core/connection/pool.js:466:61)
在 Connection.emit (events.js:210:5)
在 processMessage (/rc19_pricemongo/node_modules/mongodb/lib/core/connection/connection.js:364:10)
在 TLSSocket. (/rc19_pricemongo/node_modules/mongodb/lib/core/connection/connection.js:533:15)
在 TLSSocket.emit (events.js:210:5)
在 addChunk (_stream_readable.js:309:12)
在 readableAddChunk (_stream_readable.js:290:11)
在 TLSSocket.Readable.push (_stream_readable.js:224:10)
在 TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
ok: 0,
errmsg: '游标未找到,游标ID:7820213409290816,命名空间:db_name.collection_name',
code: 43,
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: { isGetMore: true }
rc_price.rc_price_data
。错误显示游标在尝试检索数据块时已关闭。大多数Mongo驱动程序函数都可以返回Promise以避免回调地狱。您的http处理程序已经是异步的 - 尝试使用awaits重写代码,而不是回调。此外,建议在应用程序启动时仅一次执行client.connect
。驱动程序可以管理连接池-让它这样做https://dev59.com/rmgv5IYBdhLWcg3wXPkt - Alex Blex