I am trying to iterate over a mongo DBCursor in a play framework 2.0.1 view. However, nothing is listed. Ideally I want to paginate the cursor, but even if I am able to list all objects in the cursor it will be fine.
Here is what I am doing.
In my controller I query the Mongo DB and render the DBCursor. The logs state that the cursor is not empty.
Then in the view I have the following:
@(cursor: com.mongodb.DBCursor)
@main("Title") {
<div id="objects" class="content">
<h2>Objects</h2>
@if(cursor.count() == 0) {
There are currently no objects in the data base.
} else {
@while(cursor.hasNext()) {
@cursor.next().get("name");
}
}
</div>
}
Unfortunately, nothing is listed, besides the heading when the page is rendered.
Funny enough, if I omit the @while(cursor.hasNext()) and output only the first object from the cursor, then it is rendered on the page.
Apparently something is wrong with that while. So, is it possible to render (and/or paginate) the DBCursor in the view?
Thanks!
Maybe @while doesn’t exists in scala templates!?
This kind of work is not intended to be done in the view. The view is for rendering, while the controller is for logic and accessing the database. Consider to parse the DBObjects to a list or array in the controller, and use @for to iterate over it in the view.
For pagination use something like this: