Pergunta

Having trouble with a list function I wrote using CouchApp to take items from a view that are name, followed by a hash list of id and a value to create a CSV file for the user.

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.');
    }
}

If I request _list/mylist/myview (where mylist is the list function above and the view returns as described above) with ?key="something" or ?keys=["something", "another] then it works, but remove the query string and I get the error below:

{"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"}
Foi útil?

Solução

Can't say for sure since you gave little detail, however, a probable source of problems, is the use of arrays to collect data from every row: it consumes an unpredictable amount of memory. This may explain why it works when you query for a few records, and fails when you query for all records.

You should try to arrange data in a way that eliminates the need to collect all values before sending output to the client. And keep in mind that while map and reduce results are saved on disk, list functions are executed on every single query. If you don't keep list function fast and lean, you'll have problems.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top