如何在yesod中获取发布数据?

时间:2015-03-22 21:50:42

标签: yesod yesod-forms

您好我想手动在表格中插入数据,我也使用了lookupPostParam。

这是发布新消息的代码。

      postNewsR :: Handler Html
      postNewsR = do
                now <- liftIO getCurrentTime
                newsTitle <- lookupPostParam "title"
                newsUrl <- lookupPostParam "news_url"
                newsSnapshot <- lookupPostParam "news_snopshot"
                newsArea <- lookupPostParam "news_area"
                newsSubject <- lookupPostParam "news_subject"
                newsContent <- lookupPostParam "news_content"

               newsId <- runDB $ insert News newsTitle newsUrl newsSnapshot newsContent False Nothing now Nothing

              redirect NewsR

但它给了我以下错误:

      Couldn't match type ‘Control.Monad.Trans.Reader.ReaderT
                   (PersistEntityBackend
                      (Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Bool
                       -> Maybe Int
                       -> UTCTime
                       -> Maybe UTCTime
                       -> News))
                   m0
                   (Key
                      (Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Bool
                       -> Maybe Int
                       -> UTCTime
                       -> Maybe UTCTime
                       -> News))’
          with ‘Maybe Data.Text.Internal.Text
                -> Maybe Data.Text.Internal.Text
                -> Maybe Data.Text.Internal.Text
                -> Maybe Data.Text.Internal.Text
                -> Bool
                -> Maybe a0
                -> UTCTime
                -> Maybe a1
                -> Control.Monad.Trans.Reader.ReaderT
                     (YesodPersistBackend App) (HandlerT App IO) t0’
         Expected type: Maybe Data.Text.Internal.Text
           -> Maybe Data.Text.Internal.Text
           -> Maybe Data.Text.Internal.Text
           -> Maybe Data.Text.Internal.Text
           -> Bool
           -> Maybe a0
           -> UTCTime
           -> Maybe a1
           -> YesodDB App t0
         Actual type: Control.Monad.Trans.Reader.ReaderT
             (PersistEntityBackend
                (Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Bool
                 -> Maybe Int
                 -> UTCTime
                 -> Maybe UTCTime
                 -> News))
             m0
             (Key
                (Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Bool
                 -> Maybe Int
                 -> UTCTime
                 -> Maybe UTCTime
                 -> News))
      The function insert is applied to 9 arguments,
      but its type ‘(Data.Text.Internal.Text
           -> Data.Text.Internal.Text
           -> Data.Text.Internal.Text
           -> Data.Text.Internal.Text
           -> Bool
           -> Maybe Int
           -> UTCTime
           -> Maybe UTCTime
           -> News)
          -> Control.Monad.Trans.Reader.ReaderT
               (PersistEntityBackend
                  (Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Bool
                   -> Maybe Int
                   -> UTCTime
                   -> Maybe UTCTime
                   -> News))
               m0
               (Key
                  (Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Bool
                   -> Maybe Int
                   -> UTCTime
                   -> Maybe UTCTime
                   -> News))’

我希望你能帮助我,提前致谢

修改


我在代码中更改了一些内容。这是更新。 从lookupPostParam到runInputPost

    newsTitle <- runInputPost $ ireq textField "title"
    newsUrl <- runInputPost $ ireq textField "news_url"
    newsSnapshot <- runInputPost $ ireq textField "news_snopshot"
    newsArea <- runInputPost $ ireq textField "news_snopshot"
    newsSubject <- runInputPost $ ireq textField "news_snopshot"
    newsContent <- runInputPost $ ireq textareaField "news_content"

    -- Inserting it to the table News
    newsId <- insert $ News newsTitle newsUrl newsSnapshot newsContent False Nothing now Nothing

现在它给了我2个错误:

 1. Couldn't match type ‘Control.Monad.Trans.Reader.ReaderT
                   SqlBackend m0’
          with ‘HandlerT App IO’
    Expected type: HandlerT App IO (Key News)
    Actual type: Control.Monad.Trans.Reader.ReaderT
             SqlBackend m0 (Key News) …

提前致谢。

1 个答案:

答案 0 :(得分:1)

了解您如何定义News实体...

会很有帮助

让我们来看看有问题的函数的签名:

lookupPostParam :: (MonadResource m, MonadHandler m)
                => Text
                -> m (Maybe Text)

这表示lookupPostParam需要Text个参数,返回Maybe Text,并且生活在m monad中。当您调用lookupPostParam时,<-关键字会为我们展开m monad。

中的newsTitle
newsTitle <- lookupPostParam "title"

的类型为Maybe Text。我认为您的News将其字段定义为Text类型,因此您仍需要处理Maybe,然后才能在数据库中插入值。如,

case newsTitle of
 Nothing -> redirect NewsR -- and handle the error, return 400 perhaps
 Just title -> do
   newsUrl <- lookupPostParam "news_url"
   case newsUrl of
    Nothing -> redirect NewsR -- and handle the error
    Just url -> do
      -- and so on

Maybe的返回值中解放lookupPostParam后,您应该毫不费力地将它们应用到insert

newsId <- runDB $ insert $ 
          News title url snapshot content False Nothing now Nothing

如果您仍然卡住,请查看Data.Maybe或回复此处。