0

I have the following code:

new Room(
  dbObject.get("_id").get.toString,
  List[User]() ,
  List[Message](),
  dbObject.getAs[Date]("creation").get,
  //dbObject.getAsOrElse[Option[Date]]("expires", None),
  if(dbObject.containsField("expires")) 
    Some(dbObject.getAs[Date]("expires").get) 
  else 
    None,
  List[String]())

I would like to know what i'm doing wrong on use getAsOrElse because if i use the commented line, i'll never get the value from field expires. To fix it, i need to implement that if sentence to get the value.

Thank in advance.

1 Answers1

0

As getAs[T] returns Option[T] you can just use that eg:

dbObject.getAs[Date]("expires")

Will return Some(dateValue) if the expires field is of a Date or it will return None if expires field doesn't exist or if the expires field isn't castable to a Date.

Ross
  • 17,861
  • 2
  • 55
  • 73