Frage

I have an object Foo which contains a list of of objects Bar which I get from a MongoDb using play-salat plugin. The models look like this.

case class Foo (
  @Key("_id") id: ObjectId = new ObjectId,
  bars: Option[List[Bar]] = None
)

case class Bar (
  something: String
)

The view should show a list of foo objects. I pass an iterator like this

@(foos: Iterator[Foo])

the part of the template which shows the data looks like this:

@foos.map { foo =>
  <div class="foo">@foo.id</div>
  @if(foo.bars != None) {
    <ul>
      @for( bar <- bars ) {
        <li>@bar.something</li>
      }
    </ul>
  }    
}

doing this, I get a ClassCastException:

[ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.immutable.List]

I tried other variations like this

@for( i <- 0 to foo.bars.size - 1 ) {
  <li>@foo.bars.get(i).something</li>
}

resulting in ClassCastException as well:

[ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.LinearSeqOptimized]

The question is, how can I iterate through a list of mongodb objects? I guess/hope some kind of transfer objects are not necessary.

War es hilfreich?

Lösung

Note from the salat wiki that it does not support Options containing collections.

Try instead:

case class Foo (
  @Key("_id") id: ObjectId = new ObjectId,
  bars: List[Bar] = List()
)

Andere Tipps

Options containing collections, i.e. Option[List[T]] aren't currently supported in Salat. See here for more information: https://github.com/novus/salat/wiki/SupportedTypes

Just use List, and to emulate "nothing", just initialise with List.empty[Bar].

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top