Insert in loop without repeating data NodeJS + mysql?

0

I need to enter the DESCRIPTION field in a CATEGORY table;

I have my data in JSON format (already on the server);

My loop runs the sequence of steps:

  • Checks whether the "category" field exists in the JSON object
  • Make SELECT to see if the data already exists in the database

    SELECT * FROM CATEGORIA WHERE DESCRICAO = '${categoria}' ORDER BY ID_CATEGORIA ASC LIMIT 1

  • IF the resultset returned something it takes the 1st ID_CATEGORY found, otherwise it inserts the category into the bank

    INSERT INTO categoria(descricao) VALUES ('${categoria}')

  • Return to step 1 until you finish the JSON objects

  • The problem is that when I insert a JSON the loop runs completely (that is, all objects), including SELECT normally works 1 by 1, but only after the finished loop does it execute INSERT queries.

    When I insert the next JSON that already has CATEGORY registered it does not perform the INSERT, but stores my ID_CATEGORIA in a variable as expected, normally.

    I can not insert repeated categories in the bank , this is one of the situations that I use recursion, but I will also use for others like CITY, which also can not be repeated ...

    I've tried several loop structures, eg: for, for ... of, forEach (), and libs like sync-each, sync, and a few, but all just insert the data after the loop ends (even if the query insert is inside the loop).

    NOTE: I am using the querystring and mysql lib within an HTTP server

    req.on('data', function (data) {
                    objJson = JSON.parse(data)
    
                    popularDados(0);
    
                    // Função recursiva para inserir dados no banco
                    function popularDados(x) {
                        if (x < objJson.objetos.length) {
    
                            // Populando categoria
                            processarCategoria(objJson.objetos[x]);
    
                            function processarCategoria(atualObjeto) {[...]}
    
                            popularDados(x + 1)
                         }
                    };
    
    })  
    



    UPDATE 04/09/2018

    I solved my problem for now, I switched the function to the front and now I do X post via ajax, where x is the number of objects in my JSON:

    client-side:

    function popularDados(x) {
            if (x < objetos.length) {
    
                var sendObject = objetos[x];
    
                $.ajax({
                  url: "server.js",
                  method: "POST",
                  dataType: "Json",
                  data: JSON.stringify({
                      objeto : sendObject
                  })
                })
                  .done(function( obj ) {
    
                    // Usando setTimeout() apenas para visualizar no console
                    setTimeout(function(){
    
                        popularDados(x + 1);
    
                    }, 400);
    
                })
                  .fail(function( jqXHR, textStatus ) {
                    alert( "Request failed: " + textStatus );
                });          
            }
    }
    

    server-side:

    req.on('data', function (data) {
    
            objJson = JSON.parse(data)
    
            // Visualizando o dado da chave "categoria" no console
            console.log('\nCategoria : ' + objJson.objeto.categoria)
    
    
            processarCategoria(objJson.objeto);
    
            function processarCategoria(atualObjeto) {[...]}
    
        })
    
        
    asked by anonymous 02.09.2018 / 08:23

    0 answers