У меня есть моя модель от models.persistentmodels
...
Thing
title Text
price Int
kosher Bool
optionalstuff [Text] Maybe
createdat UTCTime
updatedat UTCTime
deriving Show
...
Он содержит два поля времени: UTCTime
.
Через AJAX я получаю почти Thing
в формате JSON. Но у пользователя JSON не должно быть createdat
и updatedat
или kosher
. Значит, нам нужно их заполнить.
postNewEventR = do
inputjson <- requireCheckJsonBody :: Handler Value
...
-- get rawstringofthings from inputjson
...
let objectsMissingSomeFields = case (decode (BL.fromStrict $ TE.encodeUtf8 rawstringofthings) :: Maybe [Object]) of
Nothing -> error "Failed to get a list of raw objects."
Just x -> x
now <- liftIO getCurrentTime
-- Solution needs to go here:
let objectsWithAllFields = objectsMissingSomeFields
-- We hope to be done
let things = case (eitherDecode $ encode objectsWithAllFields) :: Either String [Thing] of
Left err -> error $ "Failed to get things because: " <> err
Right xs -> xs
Ошибка «Не удалось получить вещи» появляется здесь, потому что в проанализированных нами объектах JSON отсутствуют поля, необходимые в модели.