none
[Node.js][Mobile Apps] Problem with syntax RRS feed

  • Question

  • the next function is not working, I think is a syntax problem because console.log("inserted"); never appears.

    var item = { 
                         password : postValues.password,
                         email : postValues.email,                     
     }; 


    var accounts = currentRequest.azureMobile.tables('accounts');

                                                           

    accounts.insert(item).then(function(data){

                                console.log("inserted");

     });



    Thursday, February 18, 2016 10:34 PM

Answers

  • I reproduced your issue and debugged into the operation, got the following error message: 

    debug: SQL statement failed - Must declare the scalar variable "@id".: INSERT INTO [dbo].[accounts] ([email],[password]) VALUES (@email,@password); SELECT * FROM [dbo].[accounts] WHERE [id] = @id;  with parameters [{"name":"email","value":"email@domain,com","type":{}},{"name":"password","value":"password","type":{}}]

    Then dived into the sdk source code about how to parse items at https://github.com/Azure/azure-mobile-apps-node/blob/master/src/express/middleware/parseItem.js, found we need to create the "id" index with the insert item.

    So you can modify your code in :

    var uuid = require('node-uuid');
    var item = {
        id:uuid.v4(),
        email:req.body.email,
        password:req.body.password
    };
    var accountTable = req.azureMobile.tables('accounts');
    accountTable.insert(item).then(function(data){
        res.send(data);
     }); 

    Friday, February 19, 2016 2:53 AM
    Moderator

All replies

  • I'm having a hard time following this, so I'm going to guess. This is an azure-mobile-apps v2.0.0 Node.js site (or something you set up through Azure App Service using the Quickstart functionality) and this is a custom API (or something you set up through EasyAPI)?

    Could you post a complete API so we can see where you are going wrong?

    Thanks,

    Thursday, February 18, 2016 11:49 PM
  • yes this is with the quickstart and I am doing a custom api for register.                                                                                                                                                                                                                                                                                           

    This is all the function: 

    var jwthelper = require('./jwthelper');
    var aud = "Custom";
    var currentRequest;
    var tables = require('azure-mobile-apps/src/express/tables');


    module.exports = {
        //"get": function (req, res, next) {
        //}

        "post": function(request, response) {
            currentRequest = request;   
            var postValues = currentRequest.query;
            console.log("hola perro");

            if (postValues != null){
                console.log("no es null");
                console.log(postValues.password);
                console.log(postValues.email);
            } else {
                 console.log("hello log"); 
            }

            var accounts = currentRequest.azureMobile.tables('accounts');

            var item = { 
                         password : postValues.password,
                         email : postValues.email,

                        };   

            console.log("que tal");

            accounts.where({ email : item.email}).read().then(function(data){
                 console.log("Creating account jony");
                     console.log(data.length);
    if (data.length > 0) {
                        response.send(200, { Status : 'FAIL', Error: 'This email already exists'});
    return;
    }
    else {

                        console.log("Creating account data");

    item.salt = jwthelper.createSalt();
                        console.log(item.salt);

    jwthelper.hash(item.password, item.salt, function(err, h) {

    item.password = h;                        
                            item.status = 'NewAccount';
                            item.createDate = new Date();
                            item.updateDate = new Date();
                            console.log("entramos");


                            accounts.insert(item).then(function(){

                                console.log("insertado");
                               /*
                               var userId = aud + ":" + item.id;

                               //update our record with the user id                            
                               item.userId = userId;
                               accounts.update(item);

      // We don't want the salt or the password going back to the client
      delete item.password;
      delete item.salt;
                               delete item.status;                              

                               item.token = jwthelper.zumoJwt(aud, userId, request.service.config.masterKey);
                               item.Status = 'User registered';
                               response.send(201, item);*/

                            }).then(function(results){
                                console.log("insertado");
                            });                        

    });   
                    }
             });     

        }

    };

    Friday, February 19, 2016 1:03 AM
  • I reproduced your issue and debugged into the operation, got the following error message: 

    debug: SQL statement failed - Must declare the scalar variable "@id".: INSERT INTO [dbo].[accounts] ([email],[password]) VALUES (@email,@password); SELECT * FROM [dbo].[accounts] WHERE [id] = @id;  with parameters [{"name":"email","value":"email@domain,com","type":{}},{"name":"password","value":"password","type":{}}]

    Then dived into the sdk source code about how to parse items at https://github.com/Azure/azure-mobile-apps-node/blob/master/src/express/middleware/parseItem.js, found we need to create the "id" index with the insert item.

    So you can modify your code in :

    var uuid = require('node-uuid');
    var item = {
        id:uuid.v4(),
        email:req.body.email,
        password:req.body.password
    };
    var accountTable = req.azureMobile.tables('accounts');
    accountTable.insert(item).then(function(data){
        res.send(data);
     }); 

    Friday, February 19, 2016 2:53 AM
    Moderator
  • I tried it, but is not working yet.
    I test it this way because req.body is null, so for to get the email and password is with req.query:

    var uuid = require('node-uuid');
    var item = {
        id:uuid.v4(),
        email:req.query.email,
        password:req.query.password
    };
    var accountTable = req.azureMobile.tables('accounts');
    accountTable.insert(item).then(function(data){
        res.send(data);
     }); 

    Friday, February 19, 2016 4:47 PM
  • In order to get body parsing to work (which is required only if you are accepting POST via a Custom API), you need to do the following in the main program (app.js or server.js)

    var bodyParser = require('body-parser');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true });

    Check out the Howto handle large file imports in the Node.js Server SDK HOWTO for an example.

    Friday, February 19, 2016 6:06 PM
  • now insert is working!!! but I still can´t do it how I want, the problem happen when I try to add the salt                                           

    item.salt = jwthelper.createSalt();
    
    jwthelper.hash(item.password, item.salt, function(err, h) {
    
    	item.password = h;                        
       	 accountTable.insert(item).then(function(data){
             delete item.salt;
             res.send(data);
     }); 
    
    });
    
    
    Where createSalt is define as:
    
    var crypto = require('crypto');
    var iterations = 1000;
    var bytes = 32;
    
    exports.createSalt = function() {
    	
    	return new Buffer(crypto.randomBytes(bytes).toString('base64'));
    
    };
    


    Friday, February 19, 2016 8:23 PM
  • If the issue cannot be resolved, could you please create a new thread to discuss this issue since your new question is not directly related to the original issue, it would be better if you open up a new thread for the new question. In this way, our discussion here will not deviate too much from the original issue. This will make answer searching in the forum easier and be beneficial to other community members as well.

    Thank you for your understanding.

    Monday, February 22, 2016 8:35 AM
    Moderator