Yesod のルートに json を送信する ajax 呼び出しがあり、ルートで json を解析してデータベースに直接挿入する必要があります。私のモデルファイルには
createtime UTCTime default=now()
クライアントが作成時間を送信していないため、json の解析が妨げられています。ログエントリ用に独自の parseJson を記述しようとしましたが、getCurrentTime が IO モナドで値を返すため、UTCTime のデフォルトを挿入できませんでした。可能であれば、データベースに値を設定してもらいたいです。
この時点で考えられる唯一のことは、LogEntryWithoutTime のような型を作成し、JSON を解析して LogEntry に変換することです。もっと簡単な方法はありますか?
編集: JSON 解析に getCurrentTime を追加する 3 つの異なる失敗を示します。まず、使用可能な場合は createtime を解析し、デフォルトでサーバーの getCurrentTime に設定することを目的としています。クライアントの時間に頼るべきではないので、これはとにかく正しくありません。
instance FromJSON Log where
parseJSON (Object o) = Log
<$> o .: "userid"
...
<*> o .:? "createtime" .!= liftIO getCurrentTime
エラーは
Model.hs:58:32:
Couldn't match expected type ‘UTCTime’
with actual type ‘m0 UTCTime’
In the second argument of ‘(.!=)’, namely ‘liftIO getCurrentTime’
In the second argument of ‘(<*>)’, namely
‘o .:? "createtime" .!= liftIO getCurrentTime’
次に、現在の時刻を取得しようとします。
<*> liftIO getCurrentTime
エラーが発生します
Model.hs:58:9:
No instance for (MonadIO
aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser)
arising from a use of ‘liftIO’
In the second argument of ‘(<*>)’, namely ‘liftIO getCurrentTime’
行を次のように変更すると
<*> getCurrentTime
それから私は得る
Model.hs:58:9:
Couldn't match type ‘IO’
with ‘aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser’
Expected type: aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
UTCTime
Actual type: IO UTCTime