Home>
I want to achieve
console.log (
m_sql (
"SELECT * FROM accounts WHERE user =?;", ["Jhon"]
) .results [0] .mail_address
);
SQL statement with Node.js likem_sql ()
Pass it to the function and send the execution result of the SQL statementm_sql ()
Receive as the return value of the function.
thism_sql ()
I want to make a function.
const mysql = require ("mysql");
const connection = mysql.createConnection ({{
host: "localhost",
user: "root",
password: "toor",
database: "test"
});
/ * ================================================ ============= * /
function m_sql (code, argv) {
var ret;
new Promise ((resolve, reject) =>{
// Since connection.query is an asynchronous function, we are using a Promise.
connection.query (code, argv,
(error, results, fields) =>{
resolve ({{
error: error,
results: results,
fields: fields
});
}
);
}). then (data =>{
ret = data;
console.log (data);
// =>SQL statement execution result
console.log (ret);
// =>SQL statement execution result
// Here, the execution result of the SQL statement is safely stored in ret,
});
// Here ret is undefined
console.log (ret);
// =>undefined
return ret;
}
console.log (m_sql ("SELECT * FROM auth;"));
// =>undefined
what I want to know
- How can i
ret
Holds the execution result of the SQL statementreturn
Can you do it?
- Pass by reference
Passing JavaScript by reference was confusing and I couldn't do it.
There is also an article that passing by reference does not exist.
- OS Windows 10 Home 64bit
- Node.js v12.16.1
- SQL 10.5.5-MariaDB
-
Answer # 1
Related articles
- javascript - i want a function that returns a string to work in return
- i want to create a filter function with javascript, but i get the following error statement and cannot proceed
- ruby on rails - i want to create a like function with rails
- javascript - i want to reduce the image of the preview function
- embed a function with javascript argument in html generated from javascript
- javascript - i want to express a function registered as a value using a method name for an object with an arrow function
- javascript - i want to replace a class component with a function component
- about javascript random function
- javascript - [gas] i want to select the spreadsheet id on the html side and pass it to the function
- javascript - i want to use a function with the same name as a string that is the value of a variable in an object in vuejs
- external javascript file cannot be read from html file can't the function documentgetelementbyid be loaded?
- javascript - i want to create a table by dynamically expressing rowspan using vuejs
- c ++ - i would like to know how to create a function that overloads <<(insert operator)
- javascript - can you create a homepage using rails?
- javascript - uncaught typeerror: 〇 〇 i want to solve is not a function
- javascript - i want to create a graph and save it with the data reflected correctly
- i want to create a function that calculates the absolute value in python
- i want to create a function that passes php int type and string type and outputs the content string of the int type number of ti
- i want to change the key name part of an associative array in a javascript function
Related questions
- javascript : Webpack does not start dev server
- javascript : How to fill out the email template of the Nodemailer cyclic data?
- javascript : Electron How to change the color of the window frame in the application?
- javascript : Regex group does not work
- javascript : CREATE A NEW CSV FILE WITH NEW COLUMNS
- javascript : Transfer a variable from 1 function to another. PUPPETEER, REACTJS, Express
- SQL Error adding record
- javascript : How to perform a piece of code in Node JS asynchronous?
- javascript : How to make all actions during iterations sequentially?
- javascript : Sending data to server node.js and their processing
Promise
I think there is a misunderstanding about the behavior of.ret
The problem I'm having with is "not assigning" instead of "not holding". It may not be intuitive, but this code is processed in the following order:To solve this
m_sql ()
Asynchronize itself (Promise
Let's do it.Keep in mind when calling this function (asynchronous function)
await
WithPromise
Let's wait for the resolution:Older node versions (14.7.0 and earlier) don't support Top-level await, so the whole thing
async main
Must be enclosed in a function: