简体   繁体   English

play2框架mongodb:[NoSuchElementException:None.get]

[英]play2 framework mongodb : [NoSuchElementException: None.get]

I am having a hard time trying to create my BSON reader/writer on play 2.1.5 with mongodb. 我在尝试使用mongodb在游戏2.1.5上创建BSON读取器/写入器时遇到困难。

Here is a code excerpt in app/model/Boat.scala : 这是app / model / Boat.scala中的代码摘录:

implicit object BoatBSONReader extends BSONDocumentReader[Boat] {
 def read(doc: BSONDocument): Boat =
  Boat(
  (...)
  doc.getAs[Accomodation]("accomodation").get,
  (...)
 )
}

Then in app/model/Accomodation.scala i have : 然后在app / model / Accomodation.scala中,我有:

case class Accomodation(number_cabins: Int, cabin_configuration: Option[String], bed_configuration: Option[String], number_guest: Int)
(...)
implicit object AccomodationBSONReader extends BSONDocumentReader[Accomodation] {
 def read(doc: BSONDocument): Accomodation =
  Accomodation(
   doc.getAs[Int]("number_cabins").get,
   doc.getAs[String]("cabin_configuration"),
   doc.getAs[String]("bed_configuration"),
   doc.getAs[Int]("number_guest").get)
  }
}

I am using the "Option[String]" on some field because those keys can be missing from mongodb record. 我在某些字段上使用了“ Option [String]”,因为这些键可能会从mongodb记录中丢失。 In which case i would like to return en empty value in my scala object. 在这种情况下,我想在我的Scala对象中返回一个空值。

This is causing this error at compile time : 这在编译时导致此错误:

[NoSuchElementException: None.get]
doc.getAs[Accomodation]("accomodation").get,

Maybe someone will point me to the right direction. 也许有人会指引我正确的方向。

Thanx ! 谢谢!

This is not a compile time error, it is a runtime error that means that a document you are trying to deserialize as a Boat is not readable with your readers. 这不是编译时错误,而是运行时错误,这意味着您尝试将作为Boat进行反序列化的文档对读者不可读。

This could either be that there is no field "accomodation" in the document or that it has a value that could not be deserialized to an instance of Accomodation with your Accomodation reader. 这可能是因为文档中没有字段“ accomodation”,或者它的值无法通过您的Accomodation阅读器反序列化为Accomodation实例。 This leads to getAs[A](name) returning None . 这导致getAs[A](name)返回None

Calling .get on None gives you a NoSuchElementException in general it is considered bad practice to ever call .get on Options, instead you should always handle the two possible choices. 调用.get on None通常会给您带来NoSuchElementException ,因此,永远不要在选项上调用.get,这是一种不好的做法,相反,您应该始终处理这两种可能的选择。 You can read more about how Option works here for example: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html 您可以在此处阅读有关Option工作原理的更多信息: http : //danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html

You could possibly get more info about what is wrong by using BSONDocument.getAsTry[A](name) instead, as that returns either Success or Fail where Fail might preserve details about the error. 通过改用BSONDocument.getAsTry[A](name) ,您可能会获得更多关于错误原因的信息,因为这将返回SuccessFail ,而Fail可能会保留有关错误的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM