未请求密钥时 CouchDB 列表视图错误
Posted
技术标签:
【中文标题】未请求密钥时 CouchDB 列表视图错误【英文标题】:CouchDB list view error when no key requested 【发布时间】:2012-07-12 11:55:33 【问题描述】:我使用 CouchApp 编写的列表函数出现问题,从视图中获取名称,后跟 id 的哈希列表和为用户创建 CSV 文件的值。
function(head, req)
// set headers
start( "headers": "Content-Type": "text/csv" );
// set arrays
var snps = ;
var test = ;
var inds = [];
// get data to associative array
while(row = getRow())
for (var i in row.value)
// add individual to list
if (!test[i])
test[i] = 1;
inds.push(i);
// add to snps hash
if (snps[row.key])
if (snps[row.key][i])
// multiple call
else
snps[row.key][i] = row.value[i];
else
snps[row.key] = ;
snps[row.key][i] = row.value[i];
//send(row.key+" => "+i+" => "+snps[row.key][i]+'\n');
// if there are individuals to write
if (inds.length > 0)
// sort keys in array
inds.sort();
// print header if first
var header = "variant,"+inds.join(",")+"\n";
send(header);
// for each SNP requested
for (var j in snps)
// build row
var row = j;
for (var k in inds)
// if snp[rs_num][individual] is set, add to row string
// else add ?
if (snps[j][inds[k]])
row = row+","+snps[j][inds[k]];
else
row = row+",?";
// send row
send(row+'\n');
else
send('No results found.');
如果我使用 ?key="something" 或 ?keys=["something", "another] 请求 _list/mylist/myview(其中 mylist 是上面的列表函数并且视图返回如上所述),那么它可以工作,但删除查询字符串,我得到以下错误:
"code":500,"error":"render_error","reason":"function raised error: (new SyntaxError(\"JSON.parse\", \"/usr/local/share/couchdb/server/main.js\", 865)) \nstacktrace: getRow()@/usr/local/share/couchdb/server/main.js:865\n([object Object],[object Object])@:14\nrunList(function (head, req) var snps = ;var test = ;var inds = [];while ((row = getRow())) for (var i in row.value) if (!test[i]) test[i] = 1;inds.push(i);if (snps[row.key]) if (snps[row.key][i]) else snps[row.key][i] = row.value[i]; else snps[row.key] = ;snps[row.key][i] = row.value[i];if (inds.length > 0) inds.sort();var header = \"variant,\" + inds.join(\",\") + \"\\n\";send(header);for (var j in snps) var row = j;for (var k in inds) if (snps[j][inds[k]]) row = row + \",\" + snps[j][inds[k]]; else row = row + \",?\";send(row + \"\\n\"); else send(\"No results found.\");,[object Object],[object Array])@/usr/local/share/couchdb/server/main.js:979\n(function (head, req) var snps = ;var test = ;var inds = [];while ((row = getRow())) for (var i in row.value) if (!test[i]) test[i] = 1;inds.push(i);if (snps[row.key]) if (snps[row.key][i]) else snps[row.key][i] = row.value[i]; else snps[row.key] = ;snps[row.key][i] = row.value[i];if (inds.length > 0) inds.sort();var header = \"variant,\" + inds.join(\",\") + \"\\n\";send(header);for (var j in snps) var row = j;for (var k in inds) if (snps[j][inds[k]]) row = row + \",\" + snps[j][inds[k]]; else row = row + \",?\";send(row + \"\\n\"); else send(\"No results found.\");,[object Object],[object Array])@/usr/local/share/couchdb/server/main.js:1024\n(\"_design/kbio\",[object Array],[object Array])@/usr/local/share/couchdb/server/main.js:1492\n()@/usr/local/share/couchdb/server/main.js:1535\n@/usr/local/share/couchdb/server/main.js:1546\n"
【问题讨论】:
注意:我以前从未真正做过 javascript,所以为了确保我没有做错,代码并不像它可能的那样干净! 【参考方案1】:由于您提供的细节很少,因此无法确定,但是,问题的一个可能来源是使用数组从每一行收集数据:它消耗的内存量无法预测。这也许可以解释为什么它在查询几条记录时有效,而在查询所有记录时失败。
您应该尝试以一种无需在将输出发送到客户端之前收集所有值的方式来排列数据。请记住,虽然 map 和 reduce 结果保存在磁盘上,但列表函数会在每个查询上执行。如果你不保持列表功能的快速和精简,你就会遇到问题。
【讨论】:
以上是关于未请求密钥时 CouchDB 列表视图错误的主要内容,如果未能解决你的问题,请参考以下文章