Hello Guys我是节点js的新手,我有一个jade framerwork,我有两个html选项,我必须从不同的不同sql获取数据,如果我用一个sql和一个html选项做一切很好,但只要我执行多个SQL它给我类型错误我的js代码是:
exports.Questions = function (req, res) {
var con = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodeutility',
multipleStatements: true
});
//res.render('Subcategory', { title: 'Subcategory', year: new Date().getFullYear(), message: 'Your Subcategory page' });
con.connect();
var sql = 'SELECT * FROM mst_category';
con.query(sql, function (err, rows, fields) {
if (err) throw err;
res.render('Questions', { title: 'Users', rows: rows });
res.end;
});
var sql1 = 'SELECT * FROM mst_subcategory';
con.query(sql1, function (err, rows1, fields) {
if (err) throw err;
res.render('Questions', { title: 'Users', rows1: rows1 });
res.end;
});
con.end();
};
我的玉代码是这样的:
div.form-group
label(for='name') Select Category:
select#catname.form-control(name='catname')
each item in rows
option(value=item.id) #{item.Name}
div.form-group
label(for='name') Select Sub-Category:
select#subname.form-control(name='subname')
each item1 in rows1
option(value=item1.id) #{item1.Name}
答案 0 :(得分:2)
这可能会有所帮助:
var mysql = require('mysql')
exports.Questions = function (req, res) {
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodeutility',
multipleStatements: true
});
connection.connect();
var sql = 'SELECT * FROM mst_category';
var sql1 = 'SELECT * FROM mst_subcategory';
connection.query(sql, function (err, rows, fields) {
if (err) throw err;
connection.query(sql1, function (err, rows1, fields) {
if (err) throw err;
res.render('Questions', { title: 'Users', rows: rows ,rows1: rows1 });
res.end;
connection.end();
});
});
};
答案 1 :(得分:1)
你只需要在第一个回调函数中进行第二次查询。
exports.Questions = function(req, res) {
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'nodeutility',
multipleStatements: true
});
con.connect();
var sql = 'SELECT * FROM mst_category';
con.query(sql, function(err, rows, fields) {
if (err) throw err;
var sql1 = 'SELECT * FROM mst_subcategory';
con.query(sql1, function(err, rows1, fields) {
if (err) throw err;
res.render('Questions', { title: 'Users', rows: rows, rows1: rows1 });
});
con.end();
});
};