【发布时间】:2020-04-18 09:48:11
【问题描述】:
我的模型来自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 对象缺少模型中需要的字段。
【问题讨论】: