none
[Node.js][Mobile Services] SELECT from multitables on Custom API RRS feed

  • Question

  • Hi, i'm new to Mobile Services and i'm having a little problem on getting data from tables.
    Actually i need to select multiple itens from multiple tables, so here's what i have
    3 tables:

    Places: id, name, description, type, city, id_user.
    Images: id, id_place, img1, img2, img3.
    User: id, name, email, picture.

    here's my AngularJS code:

    client.invokeApi("buscalocalcompleto", {
        method: "get"
      }).done(function(results){
        console.log(results);
      }, function(error){
        console.log(error.message);
      });

    And my Custom API get method:

    exports.get = function(request, response) {
    var mssql = request.service.mssql;
    var q = "SELECT p.*, u.picture, u.name, u.email FROM Places as p INNER JOIN User as u ON p.id_user = u.id";
    mssql.open({
        success: function(con){
             con.query(q, {
                 success: function(results){
                     response.send(results);
                 }
             })
        },
            error: function(err) {
                console.log("error is: " + err.message);
            }
    })

    But i can't manage it to work.

    I'm building an Ionic App with JavaScript back-end for Mobile Services.

    Can someone help me work this out?



    Wednesday, May 11, 2016 7:27 PM

Answers

  • You can add parameters to the invokeApi call, have a look at https://msdn.microsoft.com/en-us/library/azure/dn306078.aspx. Your client call will look something like:

        client.invokeApi("buscalocalcompleto", { method: "GET", parameters: { param1: 'test', param2: 12.34 } })

    These are sent as querystring parameters. On the server, you can access those parameters using the req.query object:

        exports.get = function (request, response) {
            var param1 = request.query.param1,
                param2 = request.query.param2;
            // your logic here
        }

    It's worth mentioning that you can also pass more complex parameters using the 'body' option rather than the 'parameters' option, and you would then access this object using request.body instead of request.query. Keep in mind that you can't send a request body when you're using the "GET" HTTP method - you'll have to use querystring parameters in this case.

    Hope this helps!

    Wednesday, May 11, 2016 11:25 PM

All replies

  • Hi Gabriel,

    Have a look in the logs for your Mobile Service after making a request to the custom API - you should get some more detailed information about the error that is occurring.

    It's not clear from the API documentation at https://msdn.microsoft.com/en-us/library/azure/jj554212.aspx that you can call the query function without first calling open - the connection is managed for you.

    If you are starting to build a new application, we highly recommend taking a look at Azure Mobile Apps - it is essentially the newer version of the Mobile Services product and has some significant improvements. The Mobile Services product is currently being transitioned to Azure Mobile Apps and support will end in the not too distant future. Check out https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-value-prop/ and the NPM package at https://www.npmjs.com/package/azure-mobile-apps.

    Hope this helps!

    Dale

    Wednesday, May 11, 2016 9:01 PM
  • Thanks Dale, but my app is 95% done and i want to finish and present it in Mobile Services platform, then i'll migrate to Mobile Apps.
    Wednesday, May 11, 2016 10:50 PM
  • No worries, glad you have your app 95% complete!

    Are you still having the issue you initially described after removing the open statement?

    Wednesday, May 11, 2016 10:51 PM
  • IT WORKED! Thank you.
    But just one last question that is not clear for me
    How do i pass parameters to my get call?
    And do i have to declare them on my function call like:

    exports.get = function(MyParamsFromApp, request, response) {

    Wednesday, May 11, 2016 11:12 PM
  • You can add parameters to the invokeApi call, have a look at https://msdn.microsoft.com/en-us/library/azure/dn306078.aspx. Your client call will look something like:

        client.invokeApi("buscalocalcompleto", { method: "GET", parameters: { param1: 'test', param2: 12.34 } })

    These are sent as querystring parameters. On the server, you can access those parameters using the req.query object:

        exports.get = function (request, response) {
            var param1 = request.query.param1,
                param2 = request.query.param2;
            // your logic here
        }

    It's worth mentioning that you can also pass more complex parameters using the 'body' option rather than the 'parameters' option, and you would then access this object using request.body instead of request.query. Keep in mind that you can't send a request body when you're using the "GET" HTTP method - you'll have to use querystring parameters in this case.

    Hope this helps!

    Wednesday, May 11, 2016 11:25 PM