Nodejs query database execution order is not quite right. . How to synchronize queries

  node.js, question

Code directly

var resu;
 var db = require("./dbs.js");
 var http=require("http");
 var server=http.createServer(function(req,res){
 if(req.url!  =="/favicon.ico"){
 resu = db.searchDb();
 res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"*"});
 res.write(resu);
 bracket
 res.end();
 });
 server.listen(1338,"localhost",function(){
 Log ("start listening ...");
 });

This is the file queried by the database.

exports.searchDb =function(){
 var Mysql = require('node-mysql-promise');
 var mysql = Mysql.createConnection({
 host: 'dm191867.my3w.com',
 user: 'dm191867',
 password: '123',
 // database:'dm191867_db',
 });
 //SELECT * FROM table;
 mysql.table('SalesInfo').field('OrderNO,OrderDate,CustomID,Quantity,Money,OrderStatus').select().then(function (data) {
 return data;
 console.log(data);
 }).catch(function (e) {
 console.log(e);
 });
 bracket

Error message

_http_outgoing.js:439
 throw new TypeError('first argument must be a string or Buffer');
 ^
 
 TypeError: first argument must be a string or Buffer
 at ServerResponse.OutgoingMessage.write (_http_outgoing.js:439:11)
 at Server.<anonymous> (c:\Users\lenovo\WebstormProjects\bootstrap\myserver.js:8:13)
 at emitTwo (events.js:87:13)
 at Server.emit (events.js:172:7)
 at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:528:12)
 at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)

Just got into contact with nodejs and got a big head. Please ask the Great God for help!

According to the method of the great god, it has been improved, but it has encountered new problems.. The sql sentence in mysql.query(‘XXXXXX’) is fixed. I want to dynamically generate different sql statements, so I added sqldata to searchDb’s parameters.
The code is as follows:

exports.searchDb =function (sqldata,resultCallback){
 var Mysql = require('node-mysql-promise');
 var mysql = Mysql.createConnection({
 host: 'qdm19.my3w.com',
 user: 'qdm19',
 password: '123E',
 database:'qdm19_db',
 });
 //SELECT XX FROM table;
 Mysql.query('SELECT DISTINCT Percent S, Percent S, Percent S, Percent S, Percent S, Percent s FROM Percent s WHERE Percent s',sqldata).then(function (data)
 bracket
 console.log(data);
 resultCallback(null,data);
 }).catch(function (e) {
 console.log(e);
 resultCallback(e);
 });
 bracket

Where sqldata is an array

var sqldata=
 [
 'OrderNO',
 DATE_FORMAT( OrderDate, "percentage Y- percentage m- percentage d")OrderDate',
 'c.CustomName',
 '(select count(Quantity) from SalesInfo where OrderNo=s.OrderNO)Quantity',
 '(select sum(Money) from SalesInfo where OrderNo=s.OrderNO)Money',
 'OrderStatus',
 'SalesInfo as s left join Customer as c on s.CustomID=c.CustomNo',
 '1=1'
 bracket

After changing to this form, the first query will be no problem. One more query will report an error saying that there is a problem with the sql sentence.. .

var resu;
 var db = require("./dbs.js");
 var http=require("http");
 var server=http.createServer(function(req,res){
 if(req.url!  =="/favicon.ico"){
 db.searchDb(function(error,resultData){
 res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"*"});
 if(error){
 //return error message
 res.write(error);
 }else{
 res.write(resultData);
 bracket
 
 res.end();
 });
 bracket
 
 });
 server.listen(1338,"localhost",function(){
 Log ("start listening ...");
 });

Database query

exports.searchDb =function(resultCallback){
 var Mysql = require('node-mysql-promise');
 var mysql = Mysql.createConnection({
 host: 'dm191867.my3w.com',
 user: 'dm191867',
 password: '123',
 // database:'dm191867_db',
 });
 //SELECT * FROM table;
 mysql.table('SalesInfo').field('OrderNO,OrderDate,CustomID,Quantity,Money,OrderStatus').select().then(function (data) {
 //return data;
 console.log(data);
 resultCallback(null,data);
 }).catch(function (e) {
 console.log(e);
 resultCallback(e);
 });
 bracket