我在javascript中有以下代码可检索两行:
var raceCursor = RacesCollection.find({eventId: "e1"});
var race;
while(raceCursor.hasNext()){
race = raceCursor.next();
console.log(race.raceName);
}
似乎没有错,但是它显示:
在javascript控制台中
Uncaught TypeError: Object [object Object] has no method 'hasNext'
。我在这里想念的是什么?
为了在集合中使用MongoDB方法是否需要在javascript中进行特殊导入?
该集合是:
RacesCollection = new Meteor.Collection("RacesCollection");
RacesCollection.insert({raceId:"r1", eventId:"e1", raceName:"Moto race 1", status:"statusDetail"});
RacesCollection.insert({raceId:"r2", eventId:"e1", raceName:"Moto race 2", status:"statusDetail"});
任何建议都将适用。谢谢..
最佳答案
根据Meteor docs迭代游标的正确方法是cursor.forEach()
。此外,游标没有hasNext()
或next()
方法。
因此,在您的情况下,它应显示为:
var raceCursor = RacesCollection.find({eventId: "e1"});
raceCursor.forEach(function(race) {
console.log(race.raceName);
});