我对编程都很陌生。 我在我的数据库中创建了一个客户列表,并希望从中查询数据。
我不知道为什么当我连续三次运行该函数时,如果只有一个函数,它会出现错误。
var db = require('./db')
function searchClient(name, phonenumber) {
if (phonenumber == null) {
//when phonenumber is null
db.query("SELECT * FROM request WHERE name LIKE '%" + name + "%'",
function(error, results, fields) {
if (error) throw error;
//results with matched name
console.log(results)
});
} else if (name == null) {
//when name is null
db.query("SELECT * FROM request WHERE phonenumber LIKE '%" + phonenumber + "%'",
function(error, results, fields) {
if (error) throw error;
//results with matched phonenumber
console.log(results)
});
} else {
//when both are not null
db.query("SELECT * FROM request WHERE (name LIKE '%" + name + "%') AND (phonenumber LIKE '%" + phonenumber + "%')",
function(error, results, fields) {
if (error) throw error;
//results
console.log(results)
});
}
}
db.end();
console.log('nameOnly', searchClient('aaa', null));
console.log('phonenumberOnly', searchClient(null, '4056'));
console.log('nameAndPhonenumberBoth', searchClient('Engname', '3330'));
我从终端
获得此结果
nameOnly undefined
phonenumberOnly undefined
nameAndPhonenumberBoth undefined
/home/ubuntu/fixnow/api/search.js:9
if (error) throw error;
^
Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (/home/ubuntu/fixnow/node_modules/mysql/lib/protocol/Protocol.js:202:16)
at Protocol._enqueue (/home/ubuntu/fixnow/node_modules/mysql/lib/protocol/Protocol.js:135:13)
at Connection.query (/home/ubuntu/fixnow/node_modules/mysql/lib/Connection.js:208:25)
at searchClient (/home/ubuntu/fixnow/api/search.js:7:8)
at Object.<anonymous> (/home/ubuntu/fixnow/api/search.js:35:25)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
任何人都可以帮助我 如果你能提出任何简明的代码,请告诉我。