Javascript 为什么在MongoDB游标上使用.sort()会导致这个奇怪的错误?
我有以下JavaScript:Javascript 为什么在MongoDB游标上使用.sort()会导致这个奇怪的错误?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我有以下JavaScript: var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/forex-backtesting'; MongoClient.connect(url, function(err, db) { var cursor = db.collection('datapoints').find({}).sort({timestamp: 1});
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/forex-backtesting';
MongoClient.connect(url, function(err, db) {
var cursor = db.collection('datapoints').find({}).sort({timestamp: 1});
function next(error, dataPoint) {
if (!dataPoint) {
return;
}
console.log(dataPoint.data.timestamp);
cursor.nextObject(next);
}
cursor.nextObject(next);
});
我的数据点集合中有6000多个文档。当我运行此操作时,会出现以下错误:
/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
^
TypeError: Cannot read property 'timestamp' of undefined
at next (/Users/chad/development/desktop/forex-backtesting/mongodb.js:13:35)
at handleCallback (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/lib/utils.js:96:12)
at /Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/lib/cursor.js:666:5
at handleCallback (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:249:5)
at nextFunction (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:755:5)
at /Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:723:9
at queryCallback (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/wireprotocol/2_6_support.js:105:5)
at Callbacks.emit (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3)
at null.messageHandler (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:243:23)
at Socket.<anonymous> (/Users/chad/development/desktop/forex-backtesting/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:262:22)
使用此签名
var cursor = db.collection('datapoints').find({}).sort([['timestamp', 1]]);
sort不返回任何东西,它只是对数组本身进行排序。但是为什么我在使用.sort.Please发布匿名化的708文档时能够迭代708次。它发布在原始问题中。不幸的是,使用此语法时问题仍然存在。现在看起来像:var stream=DataPoint.find{symbol:self.symbol},null,{timeout:true}.sort['timestamp',1]].stream;。
var cursor = db.collection('datapoints').find({}).sort([['timestamp', 1]]);