none
[Node.js][Mobile Apps] "Cannot read property 'mssql' of undefined" RRS feed

  • Question

  • I used the next code I watch on a tutorial, I am working with iOS (swift) and when I call the next custom function:

    exports.post = function(request, response, next) {
    var mssql = request.services.mssql;
    mssql.query("update todoitem " + "set complete=1, completeDate=GETDATE() " + "where complete=0", {
            success: function(results) {
                response.send(200, "{ message: 'Hello, world!' }");
            }
        });
     }; 

    I got: 

    Optional(Error Domain=com.Microsoft.MicrosoftAzureMobile.ErrorDomain Code=-1302 "Cannot read property 'mssql' of undefined" UserInfo={com.Microsoft.MicrosoftAzureMobile.ErrorResponseKey=<NSHTTPURLResponse: 0x7fa25af3d830> { URL: https://lazarillo.azurewebsites.net/api/markalldone } { status code: 500, headers {

        "Content-Length" = 53;

        "Content-Type" = "application/json; charset=utf-8";

        Date = "Tue, 16 Feb 2016 15:37:01 GMT";

        Etag = "W/\"35-1gvX8VWObWphbnIhlDSrHw\"";

        Server = "Microsoft-IIS/8.0";

        "X-Powered-By" = "Express, ASP.NET";

    } }, NSLocalizedDescription=Cannot read property 'mssql' of undefined, com.Microsoft.MicrosoftAzureMobile.ErrorRequestKey=<NSMutableURLRequest: 0x7fa25ad0c530> { URL: https://lazarillo.azurewebsites.net/api/markalldone }})



    Tuesday, February 16, 2016 3:40 PM

Answers

  • cool! it worked I end up using it like this: 

            
    var api = {
        get: (request, response, next) => {
            var query = {
                sql: 'UPDATE TodoItem SET complete=@completed',
                parameters: [
                    { name: 'completed', value: true }
                ]
            };
            
            request.azureMobile.data.execute(query)
            .then(function (results) {
                console.log("hello log"); 
                response.send(200, "{ message: 'Hello, world!' }");
                //response.json(results);
            });
        }
    };
    
    module.exports = api;

    Thank You very much!!!!

    • Marked as answer by reneLZ Thursday, February 18, 2016 4:45 PM
    Thursday, February 18, 2016 4:44 PM

All replies

  • Are you using Azure Mobile Services (the old classic portal) or Azure App Service Mobile Apps (new new portal)?  Is your program created outside of Azure and then imported (i.e. you have all the code) or created via the quickstart?   What version of the azure-mobile-apps are you using (you will only know this if you are using the Azure app service)?T

    This will enable us to diagnose the issue.

    Tuesday, February 16, 2016 4:52 PM
  • According your endpoint, it seems that you are using Azure Mobile Apps service. And now the Mobile Apps leverage azure-mobile-apps-node sdk as a middleware in Express.js application whose architecture is different with those of Mobile services.

    Now in Mobile Apps, the request object does not contain "services" property any more, but a property object named "azureMobile" in stead.

    So, to handle sql operations in custom APIs in Mobile Apps, you can leverage azure-mobile-apps/express/tables module.  

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

    Also you can use azureMobile.data object to query data with sql stmt:

    var api = {
        get: (request, response, next) => {
            var query = {
                sql: 'UPDATE TodoItem SET complete=@completed',
                parameters: [{
                    completed: request.params.completed
                }]
            };
    
            request.azureMobile.data.execute(query)
            .then(function (results) {
                response.json(results);
            });
        }
    };
     

    You can refer to the similar thread on MSDN https://social.msdn.microsoft.com/Forums/azure/en-US/9fec53a5-a69d-4e58-a24f-a0a6224fa94f/nodejsmobile-services-to-mobile-apps-custom-api-upgrade?forum=azuremobile for your information.

    Or refer to the samples on GITHUB repositroy at https://github.com/Azure/azure-mobile-apps-node/tree/master/samples.

    Wednesday, February 17, 2016 2:27 AM
    Moderator
  • cool! it worked I end up using it like this: 

            
    var api = {
        get: (request, response, next) => {
            var query = {
                sql: 'UPDATE TodoItem SET complete=@completed',
                parameters: [
                    { name: 'completed', value: true }
                ]
            };
            
            request.azureMobile.data.execute(query)
            .then(function (results) {
                console.log("hello log"); 
                response.send(200, "{ message: 'Hello, world!' }");
                //response.json(results);
            });
        }
    };
    
    module.exports = api;

    Thank You very much!!!!

    • Marked as answer by reneLZ Thursday, February 18, 2016 4:45 PM
    Thursday, February 18, 2016 4:44 PM