节点JS DataTables和mysql

时间:2016-11-26 11:20:58

标签: javascript mysql node.js

我正在使用带有mysql的NodeJS和数据表。我无法从我的服务器获取json数据响应,这是我从console.log得到的错误:

/root/project/node_modules/mysql/lib/protocol/Parser.js:78
    throw err; // Rethrow non-MySQL errors
          ^
Error: ER_EMPTY_QUERY: Query was empty

这是我的server.js:

 var express      = require('express'),
 var mysql        = require('mysql');
 var async        = require('async'),
     QueryBuilder = require('datatable');
     var tableDefinition = {
     sTableName: 'users'
 };

var queryBuilder = new QueryBuilder(tableDefinition);

var requestQuery = {
    iDisplayStart: 0,
    iDisplayLength: 5
};
 var connection = mysql.createConnection({          // CONNECT - MySQL
    host : 'localhost',
    user : 'root',
    password : 'mydatabasepassword',
    database : 'mydatabasename',
 });

connection.connect();

/* RETURN - all users */
apiRoutes.get('/users', function(req, res) {
/* QUERY - user */
connection.query(queries.changeDatabaseOrSchema,function(err){
    if (err) throw err;

        async.parallel({
            recordsFiltered: function(cb) {
                connection.query(queries.recordsFiltered, cb);
            },
            recordsTotal: function(cb) {
                connection.query(queries.recordsTotal, cb);
            },
            select: function(cb) {
                connection.query(queries.select, cb);
            }
        }, function(err, results) {
            if (err) { 
                res.error(err);
            } else {
                res.json(queryBuilder.parseResponse(results));
            }
        });
    });
});

和users.html文件:

<script>
    $(document).ready(function() {
        var oTable = $('#users').dataTable({
            'aoColumnDefs': [
                { 'sTitle': 'ID', 'aTargets': [ 0 ], 'sWidth': '1%' },
                { 'sTitle': 'Description', 'aTargets': [ 1 ], 'sWidth': '3%' }
            ],
            'bAutoWidth': false,
            'bServerSide': true,
            'sPaginationType': 'full_numbers',
            'aaSorting': [[ 0, 'desc' ]],
            'sAjaxDataProp': 'data',
            'sAjaxSource': '/api/users?id=1',
            'oLanguage': {
                'sEmptyTable': 'No users available',
                'sLengthMenu': '_MENU_ users',
                'sInfo': 'Showing _START_ to _END_ of _TOTAL_ users',
                'sInfoFiltered': '(filtered from _MAX_ total users)',
                'sInfoEmpty': 'Showing 0 to 0 of 0 users',
                'sZeroRecords': 'No matching users found',
                'sSearch': ''
            }
        });
        /* DATATABLES - placeholder */
        $('div.dataTables_filter input').attr('placeholder', 'Search').addClass('form-control');
    });
</script>

我正在使用:        “表达”:“~4.14.0”,        “mysql”:“2.12.0”,        “async”:“~2.1.4”,        “下划线”:“~1.8.3”

更新:

我在server.js上尝试使用此代码:

/* RETURN - all users */
apiRoutes.get('/users', function(req, res) {
/* QUERY - user */
pool.getConnection(function(err,connection){
    if (err) {
       connection.release();
       res.json({"code" : 100, "status" : "Error in connection database"});
       return;
     }
        console.log(queries);
    connection.query(queries.changeDatabaseOrSchema,function(err, row){
        connection.release();
        res.json(queryBuilder.parseResponse(row));

    });
});
});

在server.js的console.log中我得到了这个:

{ recordsTotal: 'SELECT COUNT(id) FROM clients',
  select: 'SELECT * FROM dealers' }

在Chrome JSON从server.js返回时我得到了这个:

{"recordsFiltered":0,"recordsTotal":0,"draw":0}

任何人都知道如何从server.js获取有效的JSON,以便chrome上的数据表有效吗?

感谢。

0 个答案:

没有答案