2

I'm new in Yesod Haskell, I like it a lot, but I have to leave it after a month because I can not solve this problem: I have the version yesod-core version:1.0.1.3 I followed this example: More Client Side Yesod: todo sample I can create my own page and populate it with the data via json After I add a new record using json But I can not delete or change a record because I can not find a way to bring back the key. I can not use this system to derive the data as explained here: Parsing a JSON post and Correct way to do a “join” in persist with yesod and aeson-0.6.0.2: Fast JSON parsing and encoding Because I always get this error:

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 

If I use this system:

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 ()

I always get this error:

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 () }

For persistence I use MongoDB. I'll have to go back to work in Java? Thanks for any help.

Community
  • 1
  • 1

1 Answers1

1

The problem is that there's no way for GHC to know the type of id in your postAeldatidelR function. parseJsonBody_ says it must be an instance of FromJSON. delete says it must be an instance of PersistEntity. But there are potentially hundreds of instances that could match that.

In times like this, the simplest solution is to provide an explicit type signature. Perhaps this would work:

haskell runDB (delete (id :: ElarticoliId))

Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77