hasNext 不能在 javascript 中收集 [英] hasNext not working on collection in javascript
问题描述
我在 javascript 中有以下代码可以检索两行:
I have following code in javascript which retrieves two rows:
var raceCursor = RacesCollection.find({eventId: "e1"});
var race;
while(raceCursor.hasNext()){
race = raceCursor.next();
console.log(race.raceName);
}
看起来没什么问题,但它显示:
Seems nothing wrong with it, but it shows :
Uncaught TypeError: Object [object Object] 在 javascript 控制台中没有方法 'hasNext'
.
我在这里缺少什么?
MongoDB 方法是否需要在 javascript 中进行特殊导入才能用于集合??
Do the MongoDB methods requires special imports in javascript, in order to be used on the collections??
集合是:
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"});
任何建议都会被采纳.谢谢..
Any recommendation will be appriciated. thanks..
推荐答案
根据 Meteor docs 迭代一个的正确方法游标是 cursor.forEach()
.游标也没有 hasNext()
或 next()
方法.
According to Meteor docs the proper way to iterate a cursor is cursor.forEach()
. Also cursors don't have hasNext()
or next()
methods.
所以在你的情况下它应该是:
So in your case it should read:
var raceCursor = RacesCollection.find({eventId: "e1"});
raceCursor.forEach(function(race) {
console.log(race.raceName);
});
这篇关于hasNext 不能在 javascript 中收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!