Interface GET and POST method with Angular + Node.js

Asked

Viewed 543 times

2

I’m having some questions as a beginner and I wanted to help you guys out. I created a Home Automation, based on Microcontroller + Node.js + Angular.js.

Angular.js communication with the microcontroller is OK. However, I will also do the reverse. In which the MIC will send a POST command to 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 site in HTML, via Angular.js with this command:

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

**Above is in the.js code paths to 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 as follows: When I send the "QTD1LEDON" command to the POST/Output command, it sends the command to Node.js and later the site will receive the GET command with this value (GET/Saidassite) and trigger a button according to the string you received.

Ex: QTD1LED ON button triggered -> QTDLEDON -> Active Button "Toogle Bootstrap".

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

Would there be a better way to fix this code or deploy another process? Thank you!

1 answer

1

Hello Luiz, good afternoon.

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

The operator = is an assignment operator, so in the first if, it assigns "QTD1LEDON" to the date variable and as it is a defined variable it enters if. So is entering the first if ever.

I suggest also putting a console.log('data = ', date) at the beginning of the Success callback Function so you can see what came from nodejs response.

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

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.