none
[Node.js][Mobile Apps] how to handle multiple results in data.execute RRS feed

  • Question

  • I'm working on porting my Azure Mobile Services node.js backend to Azure Mobile App. I use custom APIs extensively and I use stored procedures that provide multiple results. I have code in my Azure Mobile Services backend that handles the multiple results like so:

        var iteration = 0;
        var result = { statusCode: 200, user: null };

        request.service.mssql.query(sql, params, {
            success: function (data) {
                switch (iteration) {
                    case 0: {
                        // getting the status code
                        result.statusCode = data[0].ErrorCode;
                        if (result.statusCode !== 0) {
                            response.send(statusCodes.OK, result);
                            return;
                        }
                        break;
                    }
                    case 1: {
                        // getting more data
                        result.user = data[0];
                        response.send(statusCodes.OK, result);
                        return;
                    }
                }

                iteration++;
            }
        });

    For Azure Mobile App the new code looks like this:

            var context = req.azureMobile;
            var iteration = 0;
            var result = { statusCode: 200, user: null };

            context.data.execute(query)
            .then(function (data) {
                switch (iteration) {
                    case 0: {
                        // getting the status code
                        result.statusCode = data[0].ErrorCode;

                        if (result.statusCode !== 0) {
                            res.json(result);
                            return;
                        }
                        break;
                    }
                    case 1: {
                        result.user = data[0];

                        res.json(result);
                        return;
                    }
                }

                iteration++;
            });

    The problem is that I never get the second call on the function passed to then.

    Can you help?



    Sunday, April 24, 2016 8:58 PM

Answers

  • After rereading your question, it would seem you are trying to access multiple result sets? My previous answer should get you working with SQL queries. The access multiple result sets, set the "multiple" property of the query object you pass to true, then the results passed to the success handler will be an array of result sets.

    Hope this helps!

    • Marked as answer by lucorn Monday, April 25, 2016 7:55 PM
    Monday, April 25, 2016 5:23 PM

All replies

  • It seems your code is incomplete and missing something like iteration part. According the code you provided, it will response the request in case 0 or will response a 503 timeout error, and either will execute the script in case 1, even in your mobile service custom api's code.

    Could  you please clarify your requirement?

     
    Monday, April 25, 2016 3:30 AM
    Moderator
  • Yes, it's NOT working on Azure Mobile App but the code for Azure Mobile Services is complete and it's working fine. How do I make it work for the Mobile Apps backend?
    Monday, April 25, 2016 4:30 PM
  • Hi lucorn,

    There are a couple of issues I can see - where is the query variable that is passed to context.data.execute being declared? Keep in mind this is a SQL query object, not a QueryJS query. The SQL query is documented at http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_src_data_execute.html.

    The error in this case is not being logged - you need to add a catch statement to the promise handler as there is no way to catch errors on async code. Add the following line:

    context.data.execute(query)
    .then(function (data) {})
    .catch(next);

    Hope this helps, let me know if you need further assistance.

    Monday, April 25, 2016 5:18 PM
  • After rereading your question, it would seem you are trying to access multiple result sets? My previous answer should get you working with SQL queries. The access multiple result sets, set the "multiple" property of the query object you pass to true, then the results passed to the success handler will be an array of result sets.

    Hope this helps!

    • Marked as answer by lucorn Monday, April 25, 2016 7:55 PM
    Monday, April 25, 2016 5:23 PM
  • That worked. Thank you!
    Monday, April 25, 2016 7:54 PM