0

I want to make an array of objects in node.js. I have tried this:

var dataTgl1 = "";
const dataset = []

for(var i = 0; i < data.length; i++){
   var salesman_name = data[i].sls_nama
   var salesman_code = data[i].sls_kode

   con.query('SELECT "'+salesman_code+'" as salesCode,"'+salesman_name+'" as salesName, assigned_to, created, no_spk, COUNT(id) as total from customer_prospect WHERE branch_code="'+branch_code+'" and assigned_to="'+salesman_code+'" and MONTH(created)="'+month_code+'" and YEAR(created)="'+year+'" GROUP BY assigned_to ORDER BY created asc', function(err, data) {
     console.log(this.sql);
     if(data.length==0){
        dataTgl1 = 0
        var sls_code = salesman_code;
        var sls_name = salesman_name;
     } else {
        dataTgl1 = data[0].total
        var sls_code = data[0].salesCode;
        var sls_name = data[0].salesName;
     } 
                                
     var dataset2 = {salesman_code:sls_code, salesman_name:sls_name, cabang:branch_code, tgl_1:dataTgl1}

     dataset.push(dataset2);
  });
                                 
}

console.log(dataset);

but the console.log(dataset); is show like [![this][1]][1]

my code before is :

var dataTgl1 = "";
const dataset = []

for(var i = 0; i < data.length; i++){
   var salesman_name = data[i].sls_nama
   var salesman_code = data[i].sls_kode
                     
     var dataset2 = {salesman_code:salesman_code, salesman_name:salesman_name, cabang:branch_code, tgl_1:'1'}

     dataset.push(dataset2);
  });
                                 
}

console.log(dataset);

and the console.log(dataset) is fine,like this :

[![image(2)][2]][2]

and I want array like image(2).. but i want to get data tgl_1 from query [1]: https://i.stack.imgur.com/g6OI2.png [2]: https://i.stack.imgur.com/TtR1O.png

anyone can help me? thankyou :)

Salvatore
  • 10,815
  • 4
  • 31
  • 69
Ratri
  • 337
  • 1
  • 7
  • 21
  • You can't do that: the query operation is asynchronous. You'll need to use Promises or async/await or do all your processing in the callback. – Jared Smith Oct 10 '19 at 02:20
  • The actual order is: 1. `con.query()` runs multiple times, in quick succession, 2. `console.log(dataset);` runs, printing an empty array 3. an arbitrary asynchronous SQL query finishes, and its `function(err, data) {}` callback is called 4. the other queries finish, in arbitrary order –  Oct 10 '19 at 02:23
  • Interactive consoles often don't _actually_ evaluate variables until they're expanded for performance reasons. You may want to try console logging a copy of the data instead and see if anything changes. – CollinD Oct 10 '19 at 02:29
  • @JaredSmith how can i make async? i'm newbe – Ratri Oct 10 '19 at 02:38
  • @ChrisG how can i make Complete the query in order? – Ratri Oct 10 '19 at 02:39
  • 1. promisify the query 2. use `Promise.all()` to get the results in order 3. use `async` / `await` to be able to write "synchronous" code –  Oct 10 '19 at 02:43
  • @ChrisG okay i will try it – Ratri Oct 10 '19 at 02:45
  • @Ratri that linked duplicate question has (at the time of this writing) 36 answers. That's a good place to start. – Jared Smith Oct 10 '19 at 11:15

0 Answers0