Angular,PieChart,NodeJS和多个查询

时间:2015-10-16 14:41:49

标签: sql angularjs node.js devextreme

我尝试使用多个查询将我的第二个查询设置为angular和nodejs中的饼图数据源以同时获取结果 有人有一些想法来解决它。

服务器端

var url = require('url');
//Require express, 
var express = require('express');
//and create an app
var app = express();


var mysql = require('mysql');
var connection = mysql.createConnection({
    multipleStatements: true,
    host: 'localhost',
    user: 'hello',
    password: 'passw',
    database: 'db',
    port: 330333
});


//var connection = mysql.createConnection({multipleStatements: true});

app.get('/home', function (req, res) {
    res.send('Hello World!');
});


app.get('/sts', function (req, res) {

    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Headers",
            "Origin, X-Requested-With, Content-Type, Accept");
//    res.header('Access-Control-Allow-Methods', 'POST, \n\
//                    GET, PUT, DELETE, OPTIONS');


      connection.query(   
            " SELECT ST.manufacturer, ST.name, ST.model, ST.os, ST.status, " +
            " SI.Coord, ST.Last_Update_Date_Time FROM " +
            " ( " +
            " select s.manufacturer, s.name, s.model, s.os, s. status,  " +
            "   concat(DATE(s.last_update_date),' ',TIME(s.last_update_time)) as Last_Update_Date_Time " +
            " from sts s " +
            " order by Last_Update_Date_Time DESC " +
            " ) AS ST   JOIN " +
            " ( " +
            " select DISTINCT CONCAT(si.latitude, ', ',  si.longitude) as Coord, " +
            "       concat(DATE(update_date),' ',TIME(update_time)) as Update_Date_Time " +
            " from sts_info si " +
            " order by Update_Date_Time DESC " +
            " ) AS SI ON ST.Last_Update_Date_Time = SI.Update_Date_Time; " +
            " " +
            "   \n\  " +
            "       SELECT ST.manufacturer, ST.name, ST.model, ST.os, ST.status, " +
            "       count(status) as CountStatus,  " +
            "            SI.Coord, ST.Last_Update_Date_Time FROM   " +
            "        (   " + 
            "        select s.manufacturer, s.name, s.model, s.os, s. status,   " +
            "          concat(DATE(s.last_update_date),' ',TIME(s.last_update_time)) as Last_Update_Date_Time   " +
            "        from sts s   " +
            "        order by Last_Update_Date_Time DESC  " +
            "         ) AS ST   JOIN    " +
            "        (  " + 
            "        select DISTINCT CONCAT(si.latitude, ', ',  si.longitude) as Coord,  " +
            "              concat(DATE(update_date),' ',TIME(update_time)) as Update_Date_Time  " +
            "        from sts_info si  " +
            "        order by Update_Date_Time DESC  " +
            "         ) AS SI ON ST.Last_Update_Date_Time = SI.Update_Date_Time     " +
            "         group by status; "





        , function (err, rows) {    




        if (!err) {
            console.log("Database is connected... \n");
            console.log('The solution is: ', rows[0]);
            console.log('The solution is: ', rows[1]);

        } else {
            console.log("Error connecting database... \n");
            console.log('Error while performing Query.');
        }
        console.log(rows[0]);
        console.log(rows[1]);
        res.end(JSON.stringify(rows[0]));
        res.end(JSON.stringify(rows[1]));

    });


});


var server = app.listen(8000, function () {
    var port = server.address().port;
    var host = server.address().address;
    console.log('Example app listening at http://' + host + ':' + port);
});

AngularJs

    **$scope.chartOpt1 = {
            bindingOptions: {
                dataSource: "sts"

            },**
//Exposes the current URL in the browser address bar
//Maintains synchronization between itself and the browser's URL
//Represents the URL object as a set of methods
myApp.config(function ($routeProvider) {
    $routeProvider

            // route for the home page
            .when('/', {
                templateUrl: 'pages/home.html',
                controller: 'mainController'
            })

            // route for the about page
            .when('/about', {
                templateUrl: 'pages/about.html',
                controller: 'aboutController'
            })

            // route for the contact page
            .when('/contact', {
                templateUrl: 'pages/contact.html',
                controller: 'contactController'
            })


            .when('/devicessts', {
                templateUrl: 'pages/devicessts.html',
                controller: 'devicesController'
            })

            .when('/sts', {
                templateUrl: 'pages/sts.html',
                controller: 'stsController'
            });


//  $locationProvider.html5Mode(true);
});

页面结果:

enter image description here

查看数据网格和饼图未在页面中显示。

如何将查询结果检索到角度?

谢谢

1 个答案:

答案 0 :(得分:1)

如果你想获得2个回复,你应该:

  • 发布2个请求 OR

  • 替换此

    services.AddIdentity<TUser, TRole>(o =>
    {
        o.Password.RequireUppercase = false;
        o.Password.RequireNonLetterOrDigit = false;
        o.Cookies.ApplicationCookie.LoginPath = "/Admin/Users/Login";
    });
    

由此

res.end(JSON.stringify(rows[0]));
res.end(JSON.stringify(rows[1]));

但是单个http请求无法发送2个响应