0
我有一個案例類存儲我的userOption,我插入到我的用戶。 我的用戶的結構是這樣:RuntimeException驗證JsonReaders - Scala - ReactiveMongo
{
"_id":ObjectId("55d54d05ece39a6cf774c3e4"),
"main":{
"providerId":"userpass",
"userId":"[email protected]",
"firstName":"test",
"lastName":"one",
"fullName":"Test One",
"email":"[email protected]",
"authMethod":{
"method":"userPassword"
},
"passwordInfo":{
"hasher":"bcrypt",
"password":"aslkdjfasdjh"
}
},
"userOption":{
"hotLeadNotification":{
"f":"IMMEDIATE"
}
}
}
現在
,我想添加一個附加選項:favoriteNotification。 我改變了我的情況下,類中添加favoriteNotification:
case class UserOption (
hotLeadNotification: Frequency = Frequency("IMMEDIATE"),
favoriteNotification: Frequency = Frequency("IMMEDIATE")
)
object UserOption{
implicit val userOptionFormat = Json.format[UserOption]
implicit object BSONObjectIDFormat extends Format[BSONObjectID] {
def writes(objectId: BSONObjectID): JsValue = JsString(objectId.toString())
def reads(json: JsValue): JsResult[BSONObjectID] = json match {
case JsString(x) => {
val maybeOID: Try[BSONObjectID] = BSONObjectID.parse(x)
if(maybeOID.isSuccess) JsSuccess(maybeOID.get) else {
JsError("Expected BSONObjectID as JsString")
}
}
case _ => JsError("Expected BSONObjectID as JsString")
}
}
val userOptionForm = Form(
mapping(
"hotLeadNotification" -> text,
"favoriteNotification" -> text
)((hotLeadNotification: String, favoriteNotification: String) =>
UserOption(
hotLeadNotification = Frequency(hotLeadNotification),
favoriteNotification = Frequency(favoriteNotification)
)
)((u:UserOption) => Some(u.hotLeadNotification.f, u.favoriteNotification.f))
)
implicit object UserOptionBSONReader extends BSONDocumentReader[UserOption] {
def read(doc: BSONDocument): UserOption =
UserOption(
doc.getAs[Frequency]("hotLeadNotification").getOrElse(Frequency("IMMEDIATE")),
doc.getAs[Frequency]("favoriteNotification").getOrElse(Frequency("IMMEDIATE"))
)
}
implicit object UserOptionBSONWriter extends BSONDocumentWriter[UserOption]{
def write(userOption: UserOption): BSONDocument =
BSONDocument(
"hotLeadNotification" -> userOption.hotLeadNotification,
"favoriteNotification" -> userOption.favoriteNotification
)
}
}
自從我加入favoriteNotification,我得到一個RuntimeException:
java.lang.RuntimeException: (/userOption/favoriteNotification,List(ValidationError(error.path.missing,WrappedArray())))
at scala.sys.package$.error(package.scala:27) ~[scala-library-2.11.6.jar:na]
at play.api.libs.iteratee.Iteratee$$anonfun$run$1.apply(Iteratee.scala:355) ~[play-iteratees_2.11-2.3.9.jar:2.3.9]
at play.api.libs.iteratee.Iteratee$$anonfun$run$1.apply(Iteratee.scala:348) ~[play-iteratees_2.11-2.3.9.jar:2.3.9]
at play.api.libs.iteratee.StepIteratee$$anonfun$fold$2.apply(Iteratee.scala:670) ~[play-iteratees_2.11-2.3.9.jar:2.3.9]
at play.api.libs.iteratee.StepIteratee$$anonfun$fold$2.apply(Iteratee.scala:670) ~[play-iteratees_2.11-2.3.9.jar:2.3.9]
但在我的代碼不是上市。我究竟做錯了什麼?
感謝您的幫助
什麼時候會出現錯誤?也許你是從以前保存的數據庫中獲取對象,使用不同的模式。 –
Hi Lukasz,錯誤出現在運行時,實際上是在用戶登錄時出現的。事實上,之前的架構並未包含favoriteNotification。 – odroz
所以,你不這麼認爲,當用戶想要登錄時,他的文檔需要從db中獲取,如果它是在你的midification之前創建的,它沒有添加這個字段,所以當它正在解析它沒有找到它,是什麼原因導致你的錯誤?情況會是這樣嗎? –