none
[Node.js][Mobile Apps] table.insert callback not called RRS feed

  • Question

  • Hi all,

    for some reason i'm unable to have my callbacks called when I do an insert operation on a table created with easytables, I've tried different configurations of callbacks.

    this is my code

    module.exports = {
        "post": function (req, res) {
            //data validation  
            
            req.on('data', function(chunk) {
                console.log("Received body data:");
                
                var bodyData = JSON.parse(chunk);
                console.log(bodyData);
               
                var table =req.azureMobile.tables("appointments");
                
                table.insert(bodyData,{success:function(){
                        console.log("success");
                        res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8000');
                        res.status(200);
                    },
                    error:function(err){
                        console.error("error "+err);
                        res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8000');
                        res.status(200);
                    }
                });
            });
        }
    }

    the item inserted correctly appear in the table, but no callback is called.

    Someone know a reason for this behaviour?

    thanks



    Tuesday, April 19, 2016 9:59 AM

Answers

  • Everything in Azure Mobile Apps is promise related.  So try:

    table.insert(bodyData).then(function () {
        console.log("success");
    }).catch(function (error) {
        console.log("error: ", error);
    });

    Tuesday, April 19, 2016 7:45 PM

All replies

  • Everything in Azure Mobile Apps is promise related.  So try:

    table.insert(bodyData).then(function () {
        console.log("success");
    }).catch(function (error) {
        console.log("error: ", error);
    });

    Tuesday, April 19, 2016 7:45 PM
  • Hi Adrian,thank you. the .then form was my first implementation but I was missing the .catch to manage the error. Now I can see that there's an error during the operation even the item is inserted in the table.

    Investigating I have found that, if the table has property "autoIncrement": true when I do an insert ( also with an empty object ) the operation return this error:

    error:  { [RequestError: Error converting data type nvarchar to numeric.]
      name: 'RequestError',
      message: 'Error converting data type nvarchar to numeric.',
      code: 'EREQUEST',
      number: 8114,
      lineNumber: 1,
      state: 5,
      class: 16,
      serverName: 'agenzia20',
      procName: '',
      precedingErrors: [] }

    With "autoIncrement": false and adding [myobject].id = uuid() where uuid  is var uuid = require('node-uuid');

    seems to solve the error.

    Could be this a server issue?

    Wednesday, April 20, 2016 6:56 AM
  • In Azure Mobile Apps, the Id field is a string - you can't autoIncrement a string.   If you are inserting via Azure Mobile Apps, you don't need to specify an Id - the insert will do it for you.  The promise .then() function has a parameter called "item" that contains the object just inserted but with all the Azure Mobile Apps system properties filled in. 

    Wednesday, April 20, 2016 5:17 PM
  • Azure Mobile Apps supports numeric auto increment IDs, but if you attempt to provide a string value for this, the insert will fail. I will spend a bit of time to ensure there are no server issues here.
    Wednesday, April 20, 2016 10:31 PM
  • Yep, we have a test for this scenario - https://github.com/Azure/azure-mobile-apps-node/blob/master/test/express/integration/tables/dynamicSchema.tests.js#L81
    Wednesday, April 20, 2016 10:46 PM
  • What you say is very strange on my side, because my tests are failing in It

    What I've done is this:

    with "autoIncrement": true in the table json: 

    adding my object without id specified:

    { agency_id: '7BDC3A43-7F32-4558-A58E-C7FCCA596BD0',
      nominativo: 'Test',
      motivo: 'this is a test',
      data: '2016-09-18',
      in_agenzia: true,
      recapito_cliente: 'telefono1: telefono2: email:test@test.com' }
    error RequestError: Error converting data type nvarchar to numeric.

    adding my object with id specified

    { agency_id: '7BDC3A43-7F32-4558-A58E-C7FCCA596BD0',
      nominativo: 'Test',
      motivo: 'this is a test',
      data: '2016-09-18',
      in_agenzia: true,
      recapito_cliente: 'telefono1: telefono2: email:test@test.com',
      id: '830bb3c3-9fb8-43a2-9c1f-17ccf6448c8e' }
    error RequestError: Error converting data type nvarchar to numeric.

    also with the object in your test I receive the same error

    { string: 'test', bool: true, number: 1 }
    2016-04-21T08:11:41.452Z - info: Updating schema for table appointments
    error RequestError: Error converting data type nvarchar to numeric.

    with "autoIncrement": false

    adding my object without id

    { agency_id: '7BDC3A43-7F32-4558-A58E-C7FCCA596BD0',
      nominativo: 'Test',
      motivo: 'this is a test',
      data: '2016-09-18',
      in_agenzia: true,
      recapito_cliente: 'telefono1: telefono2: email:test@test.com' }
    error RequestError: Must declare the scalar variable "@id".

    give a error with a missing id as espected.

    finally

    { agency_id: '7BDC3A43-7F32-4558-A58E-C7FCCA596BD0',
      nominativo: 'Test',
      motivo: 'this is a test',
      data: '2016-09-18',
      in_agenzia: true,
      recapito_cliente: 'telefono1: telefono2: email:test@test.com',
      id: 'af2af02d-bda0-43d2-aaaf-181c20001b1f' }
    success.

    So on my side I've solved by not using the autoincrement feature but I can't use it because has the behaviour reported. If you think that all works fine ok, otherwise if you need to investigate more you can send me a mail.

    Thank you for your time.

    Thursday, April 21, 2016 8:21 AM
  • Hi Stefano,

    I'm still unable to reproduce the issue. Creating a simple app with an app.js like the following does not repro:

    var app = require('express')(),
        mobileApp = require('azure-mobile-apps')()

    mobileApp.tables.add('inc', { autoIncrement: true })
    app.use(mobileApp)
    app.listen(3000)

    POSTing to the inc table endpoint without an ID correctly generates a new ID. Are you sure you're not trying to insert a string value into a column that was previously inserted as a number?

    I'd recommend having a look at the actual schema that your app is using. If you're using the SQL Server data provider, just connect to the underlying database with Management Studio and look at the schema. If you're using SQLite, you might need to add a "filename" attribute to the data configuration and use a tool like DB Browser for SQLite (http://sqlitebrowser.org/) to see the schema.

    Let me know how it goes, always here to help!

    Thursday, April 21, 2016 5:37 PM
  • Hi Dale,

    I'm using DatabaseSQL v12 from what I can see in the portal dashboard ( I've not done the initial configuration ). The only other difference that I can see is that I don't add the property from the code but It come from the json describing the table properties.

    I's sure that I'm not adding any string as number, also because I have done some tests by removing a field at a time ti find witch one gave me the error, and at the end I had an empty object inserted in the table but still the error. The strange thing is that the entry appear in the table, so on db side seems that the insertion was completed correctly, but the service interface give me that error.

    Friday, April 22, 2016 6:50 AM
  • Hey Stefano,

    If you would be willing to share your code, I would be more than happy to try and diagnose the issue.

    Cheers!

    Dale

    Friday, April 22, 2016 8:16 PM
  • Hi Dale,

    there's no much code more than the one I've posted that is involved to reproduce the bug, In fact the code ( fixed from your suggestion ) call the service as explained and the bug is there. I'm not the owner of the project on Azure and then I can't share it but if you want contact me with a private message I could give you project name and database info to let you do some tests.

    Tuesday, April 26, 2016 7:24 AM