Error about 'invalid JSON' with couchDB vi

2019-03-13 07:03发布

I am trying to setup the following view on CouchDB

{
"_id":"_design/id",
"_rev":"1-9be2e55e05ac368da3047841f301203d",
"language":"javascript",
    "views":{ "by_id":{
              "map" : "function(doc) { emit(doc.id, doc)}"
        },"from_user_id":{
            "map" : "function(doc) { if (doc.from_user_id) {emit(doc.from_user_id, doc)}}"},
        "from_user":{
            "map" : "function(doc) { if (doc.from_user) {emit(doc.from_user, doc)}}"},
        "to_user_id":{
            "map" : "function(doc) {if (doc.to_user_id){ emit(doc.to_user_id, doc)}}"},
        "to_user":{
            "map" : "function(doc) {if (doc.to_user){ emit(doc.to_user, doc)}}" },
        "max_id":{
         "map" : "function(doc) { if (doc.id) {emit(doc._id, eval(doc.id))}}",  
         "reduce" :"function(key,value) { a = value[0]; for (i=1; i <value.length; ++i){a =    Math.max(a,value[i])} return a}"
        }
    }
}

when I try to 'PUT' this using curl:

 curl -X PUT -d keys.json  $CDB/_design/id
 {"error":"bad_request","reason":"invalid UTF-8 JSON"}

I know it's not invalid JSON, because I tested it using the 'json' library built into Python 2.6, it loads fine. JS screw ups give me the error 'must evaluate to a function'

I have checked the file with od, there are no hidden control chars, my system is set to UTF-8. I'm using CouchDB version 0.10.1

What else might be wrong with it?

4条回答
smile是对你的礼貌
2楼-- · 2019-03-13 07:23

Did you update CouchDB from source recently? If so, be sure to remove all old files.

查看更多
相关推荐>>
3楼-- · 2019-03-13 07:26

@titanoba hinted at the problem:

The -d option of curl expects the actual data as the argument!

If you want to provide the data in a file, you need to prefix it with @:

curl -X PUT -d @keys.json  $CDB/_design/id
查看更多
We Are One
4楼-- · 2019-03-13 07:29

It might be necessary to put your JSON into single quotes:

curl -vX PUT http://localhost:5984/dbname/docid -d '{"foo" : "bar"}'

works for me but

curl -vX PUT http://localhost:5984/dbname/docid -d {"foo" : "bar"}

throws the error you mention. I guess the shell somehow interferes with the data you send when you omit the single quotes.

edit: I'm using bash.

查看更多
一纸荒年 Trace。
5楼-- · 2019-03-13 07:38

The reason

curl -vX PUT http://localhost:5984/dbname/docid -d {"foo" : "bar"}

Doesn't work is that the quotes are interpolated by the shell using the single quotes escapes the quotes.

查看更多
登录 后发表回答