1

我收到一个看起来像基本插入的错误。相关代码是

newConn = runIOE $ connect $ host "127.0.0.1"
run pipe act = access pipe master "MyDB" act

newRecord :: Pipe -> Value -> Value -> IO (Either Failure Value)
newRecord pipe fname lname = run pipe $ insert "people" ["name" := fname, "lastName" := lname]

当我跳进 GHCi 并跑步时

:set -XOverloadedStrings
pipe <- newConn
newRecord pipe "Inai" "mathi"

我得到错误

<interactive>:95:16:
    No instance for (Data.String.IsString Database.MongoDB.Value)
      arising from the literal `"Inai"'
    Possible fix:
      add an instance declaration for
      (Data.String.IsString Database.MongoDB.Value)
    In the second argument of `newRecord', namely `"Inai"'
    In the expression: newRecord pipe "Inai" "mathi"
    In an equation for `it': it = newRecord pipe "Inai" "mathi"

根据thisthis,除非我遗漏了什么,否则应该可以。

有什么提示吗?

4

2 回答 2

2

仔细查看您提到的两个链接。Haskell MongoDB 绑定提供了两个看起来非常相似的运算符:

(:=) :: Label -> Value -> Field
(=:) :: Val v => Label -> v -> Field

The second form is overloaded and more flexible to use. Anything that is in the Val class, and this includes Strings, can be passed as the second argument. So you probably want to change the occurrences of := in newRecord by =: (and adapt the type signature accordingly). Then your example will work.

于 2013-03-08T08:42:36.073 回答
1

newRecord 期望fnamelname具有 type Value。您传入了两个重载String的 s,但 GHC 找不到 for 的实例IsStringValue将这些重载String的 s 转换为Values。

我看到Value有一个构造函数String :: Text -> Value,看看它是否编译:

newRecord :: Pipe -> Text -> Text -> IO (Either Failure Value)
newRecord pipe fname lname = run pipe $ insert "people" ["name" := String fname, "lastName" := String lname]
于 2013-03-08T04:33:13.070 回答