Returning Two Arrays from Controller

30 views
Skip to first unread message

justi...@gmail.com

unread,
Dec 7, 2015, 4:16:26 PM12/7/15
to total.js - web framework for node.js
Hi all,

I am brand new to this and have the following code...


-------------------------------------------------------------
function view_index() {

var self = this;

DATABASE(function(err, connection){

if(err != null) {
self.throw500(err);
return;
}

connection.query('SELECT view5142_store.name, view5142_ticket.date, SUM(view5142_ticket.guest_count) AS guest_count, SUM(view5142_ticket.entree_count) AS entree_count, SUM(view5142_ticket.bev_count) AS bev_count, SUM(view5142_ticket.items) AS item_sales, SUM(view5142_ticket.discount) AS total_discounts, SUM(view5142_ticket.hash) AS hash_sales, SUM(view5142_ticket.hash_discount) AS hash_discount, SUM(view5142_ticket.tip) AS total_tips, SUM(view5142_ticket.tax1) as total_tax, COUNT(view5142_ticket.date) AS total_checks FROM view5142_store INNER JOIN view5142_ticket ON view5142_store.mid = view5142_ticket.store_mid WHERE view5142_ticket.date = "2015-12-02" GROUP BY view5142_store.name, view5142_ticket.date',
function(err, rows) {

connection.release();

if (err != null) {
self.view500(err);
return;
}
//return rows;
self.view('index', rows);
});

connection.query('SELECT view5142_store.name FROM view5142_store',
function(err, rows1) {

//connection.release();

if (err != null) {
self.view500(err);
return;
}

self.view('index', rows1);
console.log(rows1);
});
});
}
-------------------------------------------------------------

What I am trying to do is return 1 array with the guest counts, sales, etc and another array of labor dollars (I know the 2nd one returns the store name right now, just wanted a quick thing to test).

The goal is to be able to access them like so..
@{model[0].item_sales}
@{model2[0].name}

Peter Širka

unread,
Dec 7, 2015, 4:26:02 PM12/7/15
to tot...@googlegroups.com, justi...@gmail.com
function view_index() {


   
var self = this;

   
var model = {};


    DATABASE
(function(err, connection){


       
if (err)
           
return self.throw500(err);
                 
        connection
.query('SELECT view5142_store.name, view5142_ticket.date, SUM(view5142_ticket.guest_count) AS guest_count, SUM(view5142_ticket.entree_count) AS entree_count, SUM(view5142_ticket.bev_count) AS bev_count, SUM(view5142_ticket.items) AS item_sales, SUM(view5142_ticket.discount) AS total_discounts, SUM(view5142_ticket.hash) AS hash_sales, SUM(view5142_ticket.hash_discount) AS hash_discount, SUM(view5142_ticket.tip) AS total_tips, SUM(view5142_ticket.tax1) as total_tax, COUNT(view5142_ticket.date) AS total_checks FROM view5142_store INNER JOIN view5142_ticket ON view5142_store.mid = view5142_ticket.store_mid WHERE view5142_ticket.date = "2015-12-02" GROUP BY view5142_store.name, view5142_ticket.date', function(err, rows) {
           
           
if (err) {
                connection
.release();
               
return self.throw500(err);
           
}


            model
.a = rows;            
            connection
.query('SELECT view5142_store.name FROM view5142_store', function(err, rows) {
                connection
.release();
               
if (err)
                   
return self.throw500(err);
                model
.b = rows;
               
self.view('index', model);
           
});
       
});
   
});
}


// Try my SQL AGENT
// https://github.com/petersirka/node-sqlagent

function view_index() {
   
var self = this;

   
var sql = DB();
    sql
.query('a', 'SELECT view5142_store.name, view5142_ticket.date, SUM(view5142_ticket.guest_count) AS guest_count, SUM(view5142_ticket.entree_count) AS entree_count, SUM(view5142_ticket.bev_count) AS bev_count, SUM(view5142_ticket.items) AS item_sales, SUM(view5142_ticket.discount) AS total_discounts, SUM(view5142_ticket.hash) AS hash_sales, SUM(view5142_ticket.hash_discount) AS hash_discount, SUM(view5142_ticket.tip) AS total_tips, SUM(view5142_ticket.tax1) as total_tax, COUNT(view5142_ticket.date) AS total_checks FROM view5142_store INNER JOIN view5142_ticket ON view5142_store.mid = view5142_ticket.store_mid WHERE view5142_ticket.date = "2015-12-02" GROUP BY view5142_store.name, view5142_ticket.date');
    sql
.query('b', 'SELECT view5142_store.name FROM view5142_store');
    sql
.exec(self.callback('index'));


   
/*
    self.callback('index') is same as:
    sql.exec(function(err, response) {
        if (err)
            return self.throw500(err);
        self.view('index', response);
    });
    */

}

And view:

<div>@{model.a.length}</div>
<div>@{model.b.length}</div>


Thanks :-)

justi...@gmail.com

unread,
Dec 8, 2015, 12:52:28 AM12/8/15
to total.js - web framework for node.js, justi...@gmail.com
Thanks for the quick response and tutorial! I will give it a shot tonight when I get home :)

Justin Meier

unread,
Dec 19, 2015, 7:41:56 PM12/19/15
to total.js - web framework for node.js
Worked, thank you.. I'm starting to get the hang of this!
Reply all
Reply to author
Forward
0 new messages