0

我正在尝试将我的第二个查询设置为 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);
});

页面结果:

在此处输入图像描述

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

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

谢谢

4

1 回答 1

1

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

  • 发布 2 请求或

  • 替换这个

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

这样

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

但是一个http请求不能发送2个响应

于 2015-10-16T15:14:33.087 回答