Yesod Single RawSql并将其作为JSON格式返回

时间:2015-05-08 07:33:37

标签: haskell yesod rawsql

希望你能帮助我。

我有一个rawSql:

latestPrice :: Handler [(Single PricesId, Single PersistValue, Single    PersistValue, Single PersistValue, Single PersistValue, Single PersistValue)]
latestPrice = do
  runDB $ rawSql qryStr []
        where qryStr = " SELECT prices.id,\
                            \CONCAT(cities.name, '(', states.code, '), ', countries.name) as city, \
                            \prices.s_id, \
                            \prices.s_name, \
                            \prices.quality, \
                            \AVG(prices.amount) as amount \
                    \FROM prices \
                    \INNER JOIN cities ON cities.id = prices.city_id \
                    \INNER JOIN states ON states.id = cities.state_id \
                    \INNER JOIN countries ON countries.id = states.country_id \
                    \WHERE prices.city_id > 0 \
                    \GROUP BY prices.id, city, prices.strain_id, prices.strain_name, prices.quality, prices.quality;";

我有一个调用该函数的处理程序,应该返回一个JSON格式:

getLatestPriceSubmissionR:: Handler (Value)
getLatestPriceSubmissionR  = do
    results <- latestPrice
    return map (
     \(Single id, Single city, Single s_name, Single strain_id, Single quality, Single amount) -> [ "id" .= id,"city" .=  city,"s_id" .=  s_id,"s_name" .=  s_name, "quality" .= quality,"amount" .= amount ]
   ) results

我的问题是:  我在Handler getLatestPriceSubmissionR中的代码是否正确?  它确实只给我一条警告信息,而不是错误信息:

Couldn't match type [[aeson-0.8.0.0:Data.Aeson.Types.Internal.Pair]]
          with Value
Expected type: HandlerT App IO Value
Actual type: HandlerT
             App IO [[aeson-0.8.0.0:Data.Aeson.Types.Internal.Pair]] …

No instance for (ToTypedContent [Value])
arising from a use of yesodRunner

希望你帮帮我。

提前谢谢

1 个答案:

答案 0 :(得分:1)

该行

return map (\(...) -> [ ... ]) results

看起来不对,你可能意味着

return $ map (\(...) -> [ ... ]) results

此外,这会返回列表列表,而不是类型签名中的Value

此外,这是错误,而不是警告:

Couldn't match type [[aeson-0.8.0.0:Data.Aeson.Types.Internal.Pair]]
          with Value
Expected type: HandlerT App IO Value
Actual type: HandlerT
             App IO [[aeson-0.8.0.0:Data.Aeson.Types.Internal.Pair]] …