大家好,
請問我已取得資料庫的資料,
我該如何將資料傳給前端?
const connectFunction = (query) => {
connection.query(query, (error, results, fields) => {
if (error) throw error
//抓不到這個 JSON.parse(JSON.stringify(results))
return JSON.parse(JSON.stringify(results))
})
connection.end(function (error) {
if (error) throw error
});
}
const read = ()=>{
let query ='.......'
return connectFunction(query)
}
//這是graphql的
const root = {
users: () => {
//console.log(read())是undified
return read()
}
}
---------更新--------------
謝謝,我將connectFunction改成 Promise,就成功了。
const connectFunction = (query) => {
return new Promise(resolve => {
const connection = mysql.createConnection({
// connectionLimit: 1000,
host: 'localhost',
user: 'root',
password: '',
database: 'graphql'
})
connection.query(query, (error, results, fields) => {
if (error) throw error
resolve(JSON.parse(JSON.stringify(results)))
})
connection.end(function (error) {
if (error) throw error
});
})
}
const read = async()=>{
let query ='.......'
return await connectFunction(query)
}
const root = {
users: () => read()
}