2

I am trying to run multiple SQL queries.

The problem is that variables keep going out of scope due to fire-and-forget node.js uses. I am trying to work out how to do something similar to the await keyword in C#.

Here is my broken code, as is; which should make it clearer as to what I am trying to achieve:

function getTopicsForMessages(request, results)
{
    //Get topics for this message
    var queryString = "SELECT ripple_messenger.TopicTypes.name, ripple_messenger.TopicTypes.imageUri";
    queryString += " FROM ripple_messenger.MessageTopics JOIN ripple_messenger.TopicTypes";
    queryString += " ON (ripple_messenger.MessageTopics.topicId = ripple_messenger.TopicTypes.id)";
    queryString += " WHERE messageId = ?";

    for (var i = 0; i < results.length; i++)
    {
        mssql.query(queryString, [results[i].id],
        {
            success: function (topicsResults)
            {
                results[i].topics = topicsResults;
            },
            error: function (err) {
                console.error("SQL Read error: " + err);
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, "SQL Query failed on read");
            }
        });
    } 
    //Continue... once all queries have executed successfully.
}

I think this is just a problem of not quite knowing the node way of doing things.

I've read about a few packages / options for Node.js which allow things similar to the await keyword, but I'm not sure if I would be able to use them seens as I am using Azure Mobile Services.

I'm also sure I could somehow do this query better in SQL. But again, I'm not quite sure how.

Many thanks in advance,

Danny

4

1 回答 1

0

下面的代码显示了一种可能的实现。由于 node.js 中的所有 DB 调用都是异步的,如果您将它们运行到“常规”for 循环中,您将同时将所有调用发送到 DB,这可能会触发一些配额。下面的代码一次发送一个查询(通过等待一个调用完成再调用下一个)。您还可以并行发送一些查询(使用类似于本文中显示的批处理技术

function getTopicsForMessages(request, results)
{
    //Get topics for this message
    var queryString = "SELECT ripple_messenger.TopicTypes.name, ripple_messenger.TopicTypes.imageUri";
    queryString += " FROM ripple_messenger.MessageTopics JOIN ripple_messenger.TopicTypes";
    queryString += " ON (ripple_messenger.MessageTopics.topicId = ripple_messenger.TopicTypes.id)";
    queryString += " WHERE messageId = ?";

    var queryNextItem = function(i) {
        if (i >= results.length) {
            // All done
            continuation();
        } else {
            mssql.query(queryString, [results[i].id],
            {
                success: function (topicsResults)
                {
                    results[i].topics = topicsResults;
                    queryNextItem(i + 1);
                },
                error: function (err) {
                    console.error("SQL Read error: " + err);
                    request.respond(statusCodes.INTERNAL_SERVER_ERROR, "SQL Query failed on read");
                }
            });
        }
    }

    queryNextItem(0);

    function continuation() {
        //Continue... once all queries have executed successfully.
    }
}
于 2014-07-25T21:45:42.377 回答