在使用params的couchdb过滤复制期间出错

时间:2011-10-17 01:54:04

标签: couchdb replication

我正在尝试在两台不同的计算机上运行过滤复制,我意识到只有在执行拉复制时才会发生这种情况,如果我执行推送复制它会正常工作。

curl -X POST http://localhost:5984/_replicate -d '{\"source\":\"http://MARTIN-NEWPC:5984/pdlib\",\"target\":\"pdlib\",\"filter\":\"replication/SINGLE_COLLECTION\",\"query_params\":{\"key\":\"bb579347-9bfb-4dda-84eb-622b43108872\"}}' -H "Content-Type: application/json"

我从该请求得到的神秘回应是:

{"error":"json_encode", "reason":"{bad_term, <0.20050.0>}"}

目标couchdb日志文件中的调试输出是:

[Mon, 17 Oct 2011 01:20:48 GMT] [debug] [<0.476.0>] 'GET' /pdlib/_changes?key=bb579347-9bfb-4dda-84eb-622b43108872&filter=replication/SINGLE_COLLECTION&style=all_docs&heartbeat=10000&since=0&feed=normal {1,
                                                                                                                                                        1}
Headers: [{'Accept',"application/json"},
          {'Content-Length',"0"},
          {'Host',"MARTIN-NEWPC:5984"},
          {'User-Agent',"CouchDB/1.0.2"}]

[Mon, 17 Oct 2011 01:20:48 GMT] [debug] [<0.476.0>] OAuth Params: [{"key","bb579347-9bfb-4dda-84eb-622b43108872"},
               {"filter","replication/SINGLE_COLLECTION"},
               {"style","all_docs"},
               {"heartbeat","10000"},
               {"since","0"},
               {"feed","normal"}]

[Mon, 17 Oct 2011 01:20:48 GMT] [info] [<0.476.0>] 192.168.2.3 - - 'GET' /pdlib/_changes?key=bb579347-9bfb-4dda-84eb-622b43108872&filter=replication/SINGLE_COLLECTION&style=all_docs&heartbeat=10000&since=0&feed=normal 200

[Mon, 17 Oct 2011 01:20:48 GMT] [error] [<0.476.0>] attempted upload of invalid JSON (set log_level to debug to log it)

[Mon, 17 Oct 2011 01:20:48 GMT] [debug] [<0.476.0>] Invalid JSON: <<"bb579347-9bfb-4dda-84eb-622b43108872">>

[Mon, 17 Oct 2011 01:20:48 GMT] [info] [<0.476.0>] 192.168.2.3 - - 'GET' /pdlib/_changes?key=bb579347-9bfb-4dda-84eb-622b43108872&filter=replication/SINGLE_COLLECTION&style=all_docs&heartbeat=10000&since=0&feed=normal 400

[Mon, 17 Oct 2011 01:20:48 GMT] [debug] [<0.476.0>] httpd 400 error response:
 {"error":"bad_request","reason":"invalid UTF-8 JSON"}

如果你需要知道,这是过滤功能:

function (doc, req) {
    if (doc.type == 'collection' || doc.type == 'document') {
        for (var i in doc.path) {
            if (doc.path[i] == req.query.key) {
                return true;
            }
        }
    }
    return false;
}

关于可能原因的任何想法?

2 个答案:

答案 0 :(得分:0)

当CouchDB尝试将您的一个查询值解释为JSON(而不是原始(未引用的)字符串)时,常常会出现400“无效的UTF-8 JSON”错误。在这种情况下,复制配置会生成此HTTP请求:

GET /pdlib/_changes?key=bb579347-9bfb-4dda-84eb-622b43108872&filter=replication/SINGLE_COLLECTION&style=all_docs&heartbeat=10000&since=0&feed=normal 400

_changes feed本身不使用key参数,但正常的CouchDB _view查询会这样做 - 并且期望它是一个JSON值! - 所以你可以尝试将query_param重命名为不同的东西。

(有点不幸的是,用户定义的过滤器(和列表等)函数与CouchDB本身共享查询参数命名空间...您可能希望为自定义参数添加一些不太可能与当前或未来内置选项冲突的内容,例如myapp_key。)

答案 1 :(得分:0)

在我看来,你的JSON转义方式有问题。这对我有用:

curl -X POST http://localhost:5984/_replicate -d'{“source”:“source_db”,“target”:“target_db”,“filter”:“ddoc / filter-name”,“query_params”:{“ key“:”some_key“}}' - H”Content-Type:application / json“