【问题标题】:How do I respond with the value of a Key in Yesod?如何在 Yesod 中以 Key 的值响应?
【发布时间】:2015-06-06 22:02:28
【问题描述】:

我正在为 Yesod 中的资源编写 REST API。我有一个 POST 方法,它应该尝试创建给定的资源,如果成功返回 201 和新创建资源的 ID。例如,

postResourceR :: Handler String
postResourceR = do
  id <- runDB $ insert Resource
  sendResponseStatus status201 id -- DOES NOT COMPILE

这是我得到的错误,很简单:

No instance for (ToTypedContent (Key Resource))
  arising from use of 'sendResponseStatus'

我知道我想要ToTypedContent 的一些东西,但我找不到任何方法可以从 Database.Persist.Class 中的 Key 获得它。任何帮助将不胜感激。

编辑:

这是实际资源的定义:

Notification
    title Text
    content Text
    icon Text Maybe
    action Text Maybe 
    created UTCTime
    updated UTCTime
    deriving Show

【问题讨论】:

  • AFAIK ToTypedContent 基本上是 ByteString(see the Yesod book/docu) - 很难说你是怎么得到它的,因为我们甚至不知道你的 Key 的类型跨度>
  • 抱歉,我认为这无关紧要,因为我没有明确定义它。现在添加了我的资源的定义。
  • 具体类型也将取决于您的数据库后端等 - 无论如何您是否看过我链接的 yesod 书?基本上,如果你可以show,那么show id 应该可以工作——尽管我猜你迟早也需要另一个方向
  • 我已经阅读了这一章,但我无法将其放在一起。我现在在ToContentToTypedContent 的定义中看到了它。谢谢!

标签: haskell yesod haskell-persistent


【解决方案1】:

假设您使用的是 SQL 后端,您可以使用fromSqlKey。更一般地,您可以使用toBackendKey

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2012-12-11
    • 1970-01-01
    • 2022-11-14
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多