1

您好我想在表中手动插入数据,并且我使用了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) …

提前致谢。

4

1 回答 1

1

看看你是如何定义你的News实体会很有帮助的......

让我们看一下相关函数的签名:

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

这就是说lookupPostParam接受一个Text参数,返回一个Maybe Text,并且生活在m单子中。当您调用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或者如果您仍然卡住,请在此处回复。

于 2015-04-02T00:07:44.373 回答