我在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"});
任何建议都会受到批评。感谢..
答案 0 :(得分:7)
根据Meteor docs,迭代光标的正确方法是cursor.forEach()
。游标也没有hasNext()
或next()
方法。
所以在你的情况下它应该是:
var raceCursor = RacesCollection.find({eventId: "e1"});
raceCursor.forEach(function(race) {
console.log(race.raceName);
});