显示功能提供了json

时间:2014-01-27 16:05:53

标签: couchdb cloudant

我的CouchDB show函数不会运行provide('json',...)函数。它会在某些情况下运行html提供。这是show函数:

function(doc, req) {
  provides('json', function(){
    return {'json': doc };
  });

  provides('html', function(){
    return "<html><body>html string here</body></html>";
  });

  return {'json': {
    'hello': "goodbye"
    } 
  };

}

这是发送text / x-json时的示例请求。你好:如果我使用Accept:application / json

,也会返回再见
dave@ubuntu-laptop:~/py/liqc$ curl -i -H "Accept: text/x-json" http://127.0.0.1:8001/liqc/user-dave
HTTP/1.1 200 OK
Content-Length: 20
Vary: Accept
Server: CouchDB/1.0.2 (Erlang OTP/R14B)
ETag: "6V7EMSS64ZQ5SRLI0EYQVDWES"
Cache-Control: must-revalidate
Date: Mon, 27 Jan 2014 15:54:31 GMT
Content-Type: text/plain;charset=utf-8, text/x-json

{"hello":"goodbye"}

当我请求text / html时,我也打招呼:再见。如果我删除show函数的最终返回但是,application / json将继续给我打招呼:再见,但是text / html会给我我想要的结果!

dave@ubuntu-laptop:~/py/liqc$ curl -i -H "Accept: text/html" http://127.0.0.1:8001/liqc/user-dave
HTTP/1.1 200 OK
Content-Length: 42
Vary: Accept
Server: CouchDB/1.0.2 (Erlang OTP/R14B)
ETag: "9B8K3XGK28Y7RL2ART28WLL50"
Date: Mon, 27 Jan 2014 16:02:41 GMT
Content-Type: text/html; charset=utf-8

<html><body>html string here</body></html>

我做错了什么,或者这是CouchDB的事情吗?我正在运行localhost反向代理到cloudant BTW。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

如果您使用return,则不应使用最终providesreturn取代任何provides

另外,当您的show函数在两个不同的位置提供JSON时,您希望在请求JSON时获得什么?仅使用provides,你会没事的。

关于这个:

  

如果我删除了show函数的最终返回,那么application / json会继续给我打招呼:再见

如果您完全删除了最终回报,则无法获得"hello":"goodbye"。也许你忘了更新设计文件?调试错误的源代码可能非常令人沮丧...