Interface GET and POST Method with Angular + Node.js

2

I have some beginner's doubts and I wanted to help you. I created a Residential Automation, based on Microcontroller + Node.js + Angular.js.

Communication of the Angular.js with the Microcontroller is OK. But I will also do the reverse. In which the MIC will send a POST command to the Node.js, more or less with this parameter:

router.post('/Saidas', function(req,res) {
    saida = req.body.atuador;
    estado = req.body.estado;
    final = saida + estado;
    res.send('Enviado = Saida: ' + saida + '; Estado: ' + estado);
});

And later I will send to the HTML site, via Angular.js with this command:

router.get('/SaidasSite', function(req,res){
    res.send(final);
});

** Above is in the path.js code for Node.js.

However, in my Angular.js, I made the following code:

<script>
    var app = angular.module('Saidas',[]);
    app.controller('Status', function($scope, $http, $interval) {
        $interval(function(){
            var request = $http.get('/SaidasSite');    
            request.success(function(data) {
                if (data='QTD1LEDON') {
                    $('#QTD1LAMP').bootstrapToggle('on');
                } else (data='QTDLEDOFF'){
                    $('#QTD1LAMP').bootstrapToggle('off');
                } else (data='QTD2LEDON'){
                    $('#QTD2LAMP').bootstrapToggle('on')
                } else { $('#QTD1LAMP').bootstrapToggle('on'); }
            })
            .error(function(data){
                console.log('Error: ' + data);
            });
        }, 5000); 

        /* O segundo parâmetro "5000", diz que a função deve ser repetida a cada 
           5000 milisegundos (5 segundos) */
    });
</script>

My idea is this: When I send the "QTD1LEDON" command to the POST / Outputs command, it sends the command to the Node.js and afterwards the site will receive the GET command with that value (GET / SaidasSite ) and hit a button according to the string it received.

Ex: QTD1LED button ON ON - > QTDLEDON - > Activate "Bootstrap Toogle" button.

I'm not finding the best way to do this. This process is not working. In this code, it always fires the "QTD1LEDON".

Would you have a better way to fix this code or implement another process? Thanks!

    
asked by anonymous 04.10.2016 / 15:01

1 answer

1

Speak Luiz, good afternoon.

In your JavaScript code, in ifs, you should use the == operator or the === operator to check if the constant is equal to a variable (as is string I suggest === because it is more performative).

The operator = is the assignment operator, then in the first if, it assigns "QTD1LEDON" to the variable date and as it is a defined variable it enters the if. So it's getting into the first if ever.

I also put a console.log ('data =', date) at the beginning of the success callback function so you can see what came from the nodejs response.

Another thing is also else - when you have another condition it should be else if (condition).

I hope I have helped.

    
04.10.2016 / 18:56