locked
[Node.js][Mobile Apps] Mobile Services to Mobile Apps - Custom API Upgrade RRS feed

  • Question

  • After working with a Mobile Service for the last year, I created a new Mobile App through the portal.  It has a Node.js backend.  I want to use custom API's to access our SQL database (calling stored procedures) as I did in Mobile Services.  I get an error when using 'var mssql = request.service.mssql;' as I understand that is a Mobile Services call.  What am I missing?  (I'm a WPF .net developer, SQL developer, iOS developer, but not exactly a Node.js developer!  The only Node.js I know was for Mobile Services!)

    Wednesday, January 27, 2016 10:20 PM

Answers

  • After reaching out to the people at azure-mobile-apps-node, they gave me this custom api example which doesn't require table usage.  It also tested well in our mobile app.

        get: function (req, res, next) {
            var query = {
                sql: 'EXEC storeProcedureName @p1',
                parameters: [{ name: 'p1', value: req.query.parameterName }]
            };

            req.azureMobile.data.execute(query).then(function (results) {
                res.json(results);
            });
        }

    Thanks!

    Friday, January 29, 2016 9:11 PM

All replies

  • As the architecture changed from Mobile Service to Mobile Apps. Now the Azure Mobile Apps Node.js SDK is an express middleware package.

    In Mobile Apps, we leverage azure-mobile-apps/express/tables module to handle sql operations.

    E.G. Here is the code content in a custom api on Mobile Service:

    var tables = require('azure-mobile-apps/src/express/tables');
    module.exports = {
        "get": function (req, res, next) {
             var todoTable = req.azureMobile.tables('TodoItem');
             console.log(todoTable); 
             todoTable.read().then(function(data){
                 res.send(data);
             });     
        }
    };
    You also can refer to this thread on MSDN for your information.

    Thursday, January 28, 2016 9:43 AM
  • Thanks.  Do you know if this would work for stored procedures as well?  
    Thursday, January 28, 2016 6:22 PM
  • Yes, I think so.

    You can customize your table module scripts in "tables" folder in your root directory. And custom the table schema and operation functions in these scripts. You can refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/#TableOperations for more details.

    And here is the sample at GitHub https://github.com/Azure/azure-mobile-apps-node/tree/master/samples/personal-table for your information.


    Friday, January 29, 2016 1:49 AM
  • After reaching out to the people at azure-mobile-apps-node, they gave me this custom api example which doesn't require table usage.  It also tested well in our mobile app.

        get: function (req, res, next) {
            var query = {
                sql: 'EXEC storeProcedureName @p1',
                parameters: [{ name: 'p1', value: req.query.parameterName }]
            };

            req.azureMobile.data.execute(query).then(function (results) {
                res.json(results);
            });
        }

    Thanks!

    Friday, January 29, 2016 9:11 PM