How to bring a BD result with NODE.js into an array

Asked

Viewed 202 times

0

Personal how do I amaze, data that is in the database, in an array, with NODE.js?

I have this value this values in the database:

Column 1

(linha 1)Fortaleza – Caucaia |(linha 2)
Fortaleza – Maracanau 

Column 2

1(custo da viagem(linha 1)) | 6(tempo da viagem(linha 2))

And I’m pulling from the comic book with this code the result with the Node.js:

bd_select.js

        var mysql = require('mysql');

        var con = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "",
        database: "bd_pops"
        });

        con.connect(function(err) {
        if (err) throw err;
        con.query("SELECT * FROM tb_pops", function (err, result, fields) {
            if (err) throw err;
            console.log(result);
        });
        });

So I want to store this data, from each column, inside the arrays, which are inside the script of the.html model file, which is route[] and metrica[]. Can anyone help?

  • 1

    The result is already the very object that you expect. But this "structure" that you tried to explain in the question is very confused! By the way, I realized that you copied this example. Just check the console demonstrated that will understand. Incidentally, result is a object, not specifically a object of the Array class.

  • Yes I copied and adapted it. But when I pull it from the BD it appears the result on the console. And the first time I’m using Node.js! But obg by guidance on the result.

1 answer

1

See if that helps you:

var rota = [];
var metrica = [];
result.map(function(item){
    rota.push(item.NomeColuna1);
    metrica.push(item.NomeColuna2);
});
  • I’d put that up after con.connect.?

  • Put it in place of the console.log(result);

  • Cara worked in part. He’s repeating the data over and over and creating array within array.

  • Ok! Please put the modified code. So I can help you better.

  • Dude, I’m gonna put the code here:;var mysql = require('mysql');
 var con = mysql.createConnection({
 host: "localhost",
 user: "root",
 password: "",
 database: "bd_pops"
 });
 var rota = [];
 var metrica = [];
 
con.connect(function(err) {
 if (err) throw err;
Con.query("SELECT * FROM tb_pops", function (err, result, fields) 
if (err) throw err;
result.map(function(item){
rota.push(item.pop);
console.log(rota);
});});});

  • The result is this: [ 'POA' ]
[ 'POA', 'BEL' ]
[ 'POA', 'BEL', 'BLU' ]
[ 'POA', 'BEL', 'BLU', 'FLO' ]
[ 'POA', 'BEL', 'BLU', 'FLO', 'CUR' ]
[ 'POA', 'BEL', 'BLU', 'FLO', 'CUR', 'LON' ]
[ 'POA', 'BEL', 'BLU', 'FLO', 'CUR', 'LON', 'SPO' ]
[ 'POA', 'BEL', 'BLU', 'FLO', 'CUR', 'LON', 'SPO', 'SJC' ]
[ 'POA', 'BEL', 'BLU', 'FLO', 'CUR', 'LON', 'SPO', 'SJC', 'RJO' ]
[ 'POA', 'BEL', 'BLU', 'FLO', 'CUR', 'LON', 'SPO', 'SJC', 'RJO', 'BHO' ]
[ 'POA',
 'BEL',
 'BLU',
 'FLO',
 'CUR',
 'LON',
 'SPO',
 'SJC',
 'RJO',
'BHO',
'CAM' ]

  • Only exists in the database, for this table, 20 items.

  • var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: """, database: "bd_pops" }); var rota = []; var metrica = []; con.connect(Function(err) { if (err) throw err; Con.query("SELECT * FROM tb_pops", Function(err, result, Fields) { if (err) throw err; result.map(Function (item) { route.push(item.pop); }); ##console.log(route); }); });

  • 1

    So I put the set code up there, the console.log(route), was inside the looping, and there was a lock error of { } as well. Now you must show the correct result. Sorry for the delay.

Show 4 more comments

Browser other questions tagged

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