gpt4 book ai didi

mongodb - Play ReactiveMongo - 尝试查找一个文档时出现异常

转载 作者:IT老高 更新时间:2023-10-28 13:16:43 26 4
gpt4 key购买 nike

我已经开始使用 PlayPlay-ReactiveMongo 插件并在 GET “document by id”场景中测试 404 响应。不幸的是,我得到了这个异常,而不是 Play 返回 404 NotFound 响应:

java.util.NoSuchElementException: JsError.get
at play.api.libs.json.JsError.get(JsResult.scala:11) ~[play_2.10.jar:2.1.1]
at play.api.libs.json.JsError.get(JsResult.scala:10) ~[play_2.10.jar:2.1.1]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:44) ~[play2-reactivemongo_2.10-0.9.jar:0.9]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:42) ~[play2-reactivemongo_2.10-0.9.jar:0.9]
at reactivemongo.api.collections.GenericQueryBuilder$class.reactivemongo$api$collections$GenericQueryBuilder$$write(genericcollection.scala:323) ~[reactivemongo_2.10-0.9.jar:0.9]
at reactivemongo.api.collections.GenericQueryBuilder$class.cursor(genericcollection.scala:333) ~[reactivemongo_2.10-0.9.jar:0.9]

如果 id 参数与现有文档匹配,则下面的 getById 函数成功返回单个文档,但如果找不到文档,则“one [JsValue]”行上的异常。

路线文件:

GET       /items/:id            controllers.ItemsController.getById(id: String)

Controller 对象:

object ItemsController extends Controller with MongoController {

def itemsCollection: JSONCollection = db.collection[JSONCollection]("items")

def getById(id: String) = Action {
Async {

val query = Json.obj("_id" -> Json.obj("$oid" ->id))
val futureItem = itemsCollection.
find(query).
one[JsValue]

futureItem.map {
case Some(item) => Ok(item)
case None => NotFound(Json.obj("message" -> "No such item"))
}
}
}
}

也许我错过了文档中的某些内容?

这里记录了部分示例:

https://github.com/sgodbillon/reactivemongo-demo-app#simple-query

mandubian cost-to-coast 示例也处理 BadRequest 以及 NotFound 场景,但代码可能已过时,因为它不使用较新的 find(...).one[...] 语义?

http://mandubian.com/2013/01/13/JSON-Coast-to-Coast/#action-get

最佳答案

原来 ID 需要是一个有效的 ObjectId,例如24 个字符,没有非法标记。

  • 如果 ID 有效但未引用现有文档,那么我会得到预期的 404。
  • 如果 ID 格式无效(例如,只有 12 个字符或包含非法标记,如“@”),那么我会收到异常。

当我将行为与等效的 Node.js + Mongoose 应用程序进行比较时,结果非常相似。

例如,如果故意使用格式错误的 12 字符 ID 进行查询,我会在 Node 中得到这个堆栈跟踪:

{ message: 'Cast to ObjectId failed for value "51bded70543f" at path "_id"',
name: 'CastError',
type: 'ObjectId',
value: '51bded70543f',
path: '_id' }

不确定此异常是否也是 Play 应用中的潜在错误,但它提供了足够的线索。答案似乎是在调用 find(query).one[T] 之前预先验证 ID。

关于mongodb - Play ReactiveMongo - 尝试查找一个文档时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17126980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com