简体   繁体   English

我如何解决摘要功能代码中的“ HTTP请求失败:何时不是字段”运行时错误?

[英]How do I solve “HTTP request failed with: when is not a field” runtime errors in digestive-functors code?

I have a simple and incomplete Happstack application, that consists of a digestive-functors form defined as follows: 我有一个简单且不完整的Happstack应用程序,该应用程序由如下定义的摘要功能组成:

setRecFormSpec :: Monad m => Form Text m Reminder
setRecFormSpec = Reminder
    <$> "progname" .: string Nothing
    <*> "channel" .: string Nothing
    <*> "when" .: localTimeFormlet "%d/%m/%Y" "%H:%M" Nothing
    <*> "recordLimit" .: stringRead "Can't parse number" (Just 7)

and its view defined as follows: 其视图定义如下:

setRecView :: View H.Html -> H.Html
setRecView view = do
        H.div ! A.class_ "container" $ do
            H.h1 "Set Record Reminder"
            childErrorList "" view

            divFormGroup $ do
                label "progname" view "Program Name:"
                formControl $ inputText "progname" view

            divFormGroup $ do
                label "channel" view "Channel:"
                formControl $ inputText "channel" view

            divFormGroup $ do
                label "when" view "When:"
                formControl $ inputDate "when" view

            divFormGroup $ do
                label "recordLimit" view "Recording Limit (days):"
                formControl $ inputText "recordLimit" view

            divFormGroup $ do
                formControl $ inputSubmit "Signup"

-- divFormGroup -- candidate to go into a Bootstrap library
divFormGroup :: H.Html -> H.Html
divFormGroup h =
    H.div ! A.class_ "form-group" $ h

-- formControl -- candidate to go into a Bootstrap library
formControl :: H.Html -> H.Html
formControl h = (h ! A.class_ "form-control")

Reminder is defined as follows: 提醒定义如下:

data Reminder = Reminder {
        programName     :: String    -- ^ name of program
    ,   channel         :: String    -- ^ name of broadcast channel
    ,   firstShowing    :: LocalTime  -- ^ time of first showing
    ,   timerPeriodDays :: Integer     -- ^ how far in advance we can set timer, in days
} deriving (Show)

When I browse to the path for the form (/setrec) I get a blank page with the following error message printed on the console: 当我浏览到表单(/ setrec)的路径时,我得到一个空白页,控制台上印有以下错误消息:

HTTP request failed with: when is not a field

I've found where that error message is defined (in https://github.com/jaspervdj/digestive-functors/blob/7e50d5686abc4b39389ed195693660d758987c7c/digestive-functors/src/Text/Digestive/Form/Internal.hs ) but I can't see from there why the 'when' field would not be found. 我发现了错误消息的定义位置(在https://github.com/jaspervdj/digestive-functors/blob/7e50d5686abc4b39389ed195693660d758987c7c/digestive-functors/src/Text/Digestive/Form/Internal.hs中 ),但我可以从那里看不到为什么找不到“时间”字段。

What is the problem here? 这里有什么问题?

How do I debug this kind of issue? 如何调试此类问题?

Here's a link to the whole code on github, in case you need to look more deeply at the code: 这是指向github上整个代码的链接,以防您需要更深入地研究代码:

https://github.com/nmbooker/recremind https://github.com/nmbooker/recremind

I ran into the same error when I used utcTimeFormlet , which internally calls localTimeFormlet so it is likely that this is the same error you experienced. 我在使用utcTimeFormlet时遇到了相同的错误,该内部调用了localTimeFormlet因此很可能是您遇到的相同错误。

Take a look at the code of localTimeFormlet 看一下localTimeFormlet的代码

localTimeFormlet dFmt tFmt d = LocalTime
  <$> "date" .: dateFormlet dFmt (localDay <$> d)
  <*> "time" .: timeFormlet tFmt (localTimeOfDay <$> d)

As you can see it consists of two subFormlets, which is the reason why "when" is not a field. 如您所见,它由两个subFormlet组成,这就是"when"不是字段的原因。 It refers to the overall construct with "date" and "time" being the actual fields. 它指的是带有"date""time"作为实际字段的整体结构。

The "solution" I used was to skip this function entirely since I actually only needed a date, not a precise time. 我使用的“解决方案”是完全跳过此功能,因为我实际上只需要一个日期,而不是精确的时间。 If that's the same in your case you probably want to do so, too. 如果您的情况相同,那么您可能也想这样做。

Otherwise you will have to incorporate the two fields into your view and it should work. 否则,您将不得不将这两个字段合并到您的视图中,并且它应该起作用。

I solved it, with a variation on Pseudoradius' solution. 我解决了这个问题,对伪半径的解决方案做了一些改动。

I defined a new view thus: 我因此定义了一个新视图:

dateTimeView view = do
    divFormGroup $ do
        label "date" view "When (Date):"
        formControl $ inputText "date" view

    divFormGroup $ do
        label "time" view "When (Time):"
        formControl $ inputText "time" view

and in my form I used it with subView , thus: 在我的表单中,我将它与subView一起subView ,因此:

setRecView :: View H.Html -> H.Html
setRecView view = do
        H.div ! A.class_ "container" $ do
            -- ...

            divFormGroup $ do
                label "channel" view "Channel:"
                formControl $ inputText "channel" view

            dateTimeView $ subView "when" view

            divFormGroup $ do
                label "recordLimit" view "Recording Limit (days):"
                formControl $ inputText "recordLimit" view

            divFormGroup $ do
                formControl $ inputSubmit "Signup"

Then the runForm call was able to find the when field and get the date and time from it. 然后, runForm调用便能够找到when字段并从中获取日期和时间。

I could parameterise dateTimeView with dateLabelText and timeLabelText, and probably will, but the above should be enough for illustrative purposes. 我可以使用dateLabelText和timeLabelText参数化dateTimeView,也许可以,但是以上内容足以说明问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM