The code looks like this
const select = function () {
let sql = 'SELECT * FROM wdata';
connection.query(sql,function (err, result) {
if(err){
console.log('[SELECT ERROR] - ',err.message);
return;
}
console.log(" The query result is ",result);
// return result
});
};
let data = select()
console.log(data,'data')
Now want to get result The result value returned , How to get it ?
See what you mean , You want to ask how to get the final result asynchronously :
You get the result through the callback function : Change it
const select = function () {
return new Promise(function(resolve, reject){
let sql = 'SELECT * FROM wdata';
connection.query(sql,function (err, result) {
if(err){
console.log('[SELECT ERROR] - ',err.message);
reject(err)
return;
}
console.log(" The query result is ",result);
// return result
resolve(result)
});
})
};
What database do you use for database connection ,Sequelize
? Or what ?
connection.query
Look at this API There's no return Promise
Of , You can use it directly
const select = async function () {
let sql = 'SELECT * FROM wdata';
let res = await connection.query(sql);
return res;
}
Equivalent to the above writing , But more clearly
Access method 1:
select().then(function(data){
console.log(data,'data')
}).catch(function(err){
console.log(err)
})
Access method 2:
async function get(){
try {
let data = await select();
console.log(data,'data')
} catch (error) {
console.log(error)
}
}
get()
nodejs Configure the routing , Then request this route at the front end , But you are missing some content here ,nodejs( I am using express frame ) The last step is to respond the data back to the front end , That is, exposure to the front end , You can get the returned data through the request route , I'll post you a piece of my code ,
Encapsulated operation data file :
const mysql = require('mysql')
// Configure the basic parameters of the database
const conn = mysql.createConnection({
host: 'localhost', // Address
user: 'root', // Database user name
password: '', // Database password
database: 'BMS_db' // Database name
})
// Connect to database
conn.connect()
// Encapsulate the functions that operate the database
function sqlFn(sql, arr, callback) {
conn.query(sql, arr, (err, res) => {
if (err) {
console.log(err)
return
}
callback(res)
})
}
module.exports = sqlFn
Routing configuration file :
const express = require('express')
const router = express.Router()
const sqlFn = require('./mysqlConnect') // Introduce encapsulated database operation functions
// Get all product content
router.post('/getProducts', (req, res) => {
const sql = 'select * from product_data'
sqlFn(sql, null, result => {
res.send({
status: 200,
msg: ' Get all the data !',
result
})
})
})
Previous code snippet ( I only posted part of my code , The simplest way to query all data ) The operation that finally enables the front end to obtain data in res.send()
send Method to pass in an object , All contents in the object can be requested at the front end /getProducts
After this route, we get

You need to configure this code later post perhaps get The route of the request , And then use it res( That is to say response) Of send Method to expose the obtained data to the client .
If you don't understand anything else, you can ask again , According to your description, I can only answer so many .