【问题标题】:Yesod modify and delete by JSONYesod 通过 JSON 修改和删除
【发布时间】:2012-09-01 10:57:00
【问题描述】:

我是Yesod Haskell的新手,非常喜欢它,但是一个月后我不得不离开它,因为我无法解决这个问题: 我有版本 yesod-core version:1.0.1.3 我按照这个例子: More Client Side Yesod: todo sample 我可以创建自己的页面并通过 json 用数据填充它 在我使用 json 添加新记录之后 但是我无法删除或更改记录,因为我找不到找回密钥的方法。 我不能使用这个系统来导出数据,如下所述:Parsing a JSON post 和 Correct way to do a “join” in persist with yesod 和 aeson-0.6.0.2: Fast JSON parsing and encoding 因为我总是收到这个错误:

Exception when trying to run compile-time code:
  Data.Aeson.TH.withType: Unsupported type: TySynD Model.Elarticoli [] (AppT (ConT Model.ElarticoliGeneric) (ConT Database.MongoDB.Query.Action))
  Code: deriveFromJSON (id) ''Elarticoli 

如果我使用这个系统:

Elarticoli
   marca         Text
   descrizione   Text
   idum          Int
   prezzo        Double

instance FromJSON (Key id Elarticoli) where
    parseJSON = fmap Key . parseJSON

instance FromJSON Elarticoli where
    parseJSON (Object v) = Elarticoli
                   <$> v .: "marca"
                   <*> v .: "descrizione"
                   <*> v .: "idum"
                   <*> v .: "prezzo"
parseJSON           _  = fail "Invalid Elarticoli"

postAeldatidelR :: Handler ()
postAeldatidelR = do
    id <- parseJsonBody_
    runDB (delete id)
    sendResponseStatus status204 ()

我总是收到这个错误:

Handler/Aeldati.hs:72:12:
    Ambiguous type variable `val0' in the constraint:
      (PersistEntity val0) arising from a use of `delete'
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `runDB', namely `(delete id)'
    In a stmt of a 'do' block: runDB (delete id)
    In the expression:
      do { id <- parseJsonBody_;
           runDB (delete id);
          sendResponseStatus status204 () }

为了持久性,我使用 MongoDB。 我必须回去用Java工作吗?感谢您的帮助。

【问题讨论】:

    标签: json haskell yesod


    【解决方案1】:

    问题在于 GHC 无法知道您的 postAeldatidelR 函数中的 id 的类型。 parseJsonBody_ 表示它必须是 FromJSON 的一个实例。 delete 表示它必须是 PersistEntity 的实例。但可能有数百个实例可以与之匹配。

    在这种情况下,最简单的解决方案是提供显式类型签名。也许这会起作用:

    haskell runDB (delete (id :: ElarticoliId))

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 1970-01-01
      • 2012-09-08
      • 2017-12-16
      • 1970-01-01
      • 2019-12-12
      • 2021-05-08
      • 2015-06-09
      • 2019-09-18
      相关资源
      最近更新 更多