1

嗨,当查询在过滤后返回 0 个结果时,我收到此错误。

我该如何解决这个问题?

在此处输入图像描述

这是我的代码。

> router.post('/report/', jsonParser, function (req, res) {
var data = req.body,
    filteredID = data.filteredID,
    startRow = data.startRow,
    numRows = data.numRows,
    sortCol = data.sortCol,
    sortDir = data.sortDir;

var countQuery = 'SELECT COUNT(*) ' +
    'FROM this_view ' ;+

var query = 'SELECT * +
    'FROM this_view ' ;

var seg,
    orderBy,
    offset;

if(filteredID !== null && filteredID !== ""){
    seg = ' WHERE "ID" LIKE \'%'+ filteredID + '%\'';
    query += seg;
    countQuery += seg;
}

orderBy = ' ORDER BY UPPER(' + sortCol + ') ' + sortDir;
offset = ' OFFSET ' + startRow + ' ROWS FETCH NEXT ' + numRows + ' ROWS ONLY';

query += orderBy;
query += offset;

logger.info("Begin: " + (new Date().toString()));

async.parallel({
    rows: function (callback) {
        pool.getConnection(function (err, connection) {
            logger.info("Begin Connection: " + (new Date().toString()));
            if (err) {
                logger.error(err.message);
                return;
            }

            logger.info("Begin execute: " + (new Date().toString()));

            connection.execute(
                query,
                [],
                {
                    resultSet: true,
                    prefetchRows: 1000
                },
                function (err, result) {
                    logger.info("End execute: " + (new Date().toString()));
                    if (err) {
                        logger.error(err.message);
                        callback("Something broke in the first thing");
                        doRelease(connection);
                        return;
                    }

                    function fetchRowsFromRS(connection, resultSet, numRows) {
                        result.resultSet.getRows(
                            numRows,  // get this many rows
                            function (err, rows) {
                                if (err) {
                                    logger.error(err);
                                    doClose(connection, resultSet); // always close the result set
                                } else if (rows.length >= 0) {
                                    var procJson = [];

                                    /**
                                     *  For each row in the result, pushes a new object to the rows array
                                     *  In each new object, the key is assigned and the result row value set
                                     */
                                    for (var i = 0; i < rows.length; i++) {
                                        procJson.push({});
                                        for (var j = 0; j < resultSet.metaData.length; j++) {
                                            procJson[i][resultSet.metaData[j].name.toLowerCase()] = rows[i][j];
                                        }
                                    }

                                    //TODO: Add null handling
                                    logger.info("Send JSON: " + (new Date().toString()));
                                    callback(null, procJson);
                                    logger.info("JSON Sent: " + (new Date().toString()));
                                    if (rows.length === numRows) // might be more rows
                                        fetchRowsFromRS(connection, resultSet, numRows);
                                    else
                                        doClose(connection, resultSet); // always close the result set
                                } else { // no rows
                                    doClose(connection, resultSet); // always close the result set
                                }
                            });
                    }
                    fetchRowsFromRS(connection, result.resultSet, numRows)
                });
        });
    },
    totalRows: function (callback) {
        pool.getConnection(function (err, connection) {
            logger.info("Begin Connection: " + (new Date().toString()));
            if (err) {
                logger.error(err.message);
                return;
            }

            logger.info("Begin execute: " + (new Date().toString()));

            connection.execute(
                countQuery,
                function (err, result) {
                    logger.info("End execute: " + (new Date().toString()));
                    if (err) {
                        logger.error(err.message);
                        callback("Something broke");
                        doRelease(connection);
                        return;
                    }

                    logger.info("Send JSON: " + (new Date().toString()));
                    console.log(result.rows);
                    callback(null, result.rows[0][0]);
                    logger.info("JSON Sent: " + (new Date().toString()));

                    doRelease(connection);
                });
        });
    }
}, function(err, result){
    if(err){
        logger.error(err);
    }

    res.send(result);
});
});

如果 rows.length >= 0,我会收到此错误。如果我只使用“rows.length > 0”并且如果查询后出现 0 个结果,则它会失败。

是否有任何其他替代 async.parallel 可以解决此问题?请帮忙。

4

0 回答 0