1

我使用 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"}
4

1 回答 1

0

不能肯定地说,因为您提供的细节很少,但是,问题的一个可能来源是使用数组从每一行收集数据:它消耗不可预测的内存量。这可以解释为什么当您查询几条记录时它会起作用,而当您查询所有记录时它会失败。

您应该尝试以一种无需在将输出发送到客户端之前收集所有值的方式来排列数据。请记住,虽然 map 和 reduce 结果保存在磁盘上,但列表函数会在每个查询上执行。如果你不保持列表功能快速和精简,你就会遇到问题。

于 2012-07-12T14:01:37.823 回答