英文:
Getting an array of all rows in a MySQL table that meet a criteria
问题
我正在开发一个 Discord.js 机器人,所以我在 Node.js 中工作。我需要做的是在我的 MySQL 表中搜索并获取所有满足某个条件的行(我可以做到),然后返回所有满足该条件的行的第一列的值。我尝试了多种不同的方法,但我的主要问题是我无法返回该值。我可以在查询中使用 `console.log` 打印它,但我真的需要返回它。
```js
function fetchOrders(status){
textr = con.query(`SELECT * FROM orders WHERE OStatus = '${status}';`, function(err,rows) {
if(err){
throw err
}
var text = "";
if(rows.length<1){
text = "None"
}else{
for(i=0;i<rows.length;i++){
text = text + "`" + rows[i].OID.toString() + "`, ";
}
}
if(text===""){
text = "None"
}
return text;
})
return textr
}
text
的值是我需要的(直到 return text;
,它工作得很完美),但当我返回它时,textr
不是 text
。如果有人告诉我如何从查询中获取 text
,我将非常感激。
<details>
<summary>英文:</summary>
I am working on a discord.js bot and so I am working in Node.Js. What I need to do is search through my MySQL table and get all rows that meet a criteria (which I can do) and then return the value in first column of all those rows meeting said criteria. I have tried multiple different methods, but my main issue is I can't return that value. I can `console.log` it from within my query, but I really need to return it.
function fetchOrders(status){
textr = con.query(SELECT * FROM orders WHERE OStatus = '${status}';
, function(err,rows) {
if(err){
throw err
}
var text = "";
if(rows.length<1){
text = "None"
}else{
for(i=0;i<rows.length;i++){
text = text + "" + rows[i].OID.toString() + "
, "
}
}
if(text===""){
text = "None"
}
return text;
})
return textr
}
The value `text` is what I need (up to `return text;` it works perfectly), but when I return it `textr` is not `text`. If someone could tell me how to get `text` out of my query, I would be very grateful.
</details>
# 答案1
**得分**: 0
我假设你对Node.js中的异步调用不太熟悉。接受回调函数的调用通常是异步执行的,这意味着调用的结果不会立即可用(即使在函数调用结束后也不会),但一旦结果可用,回调函数就会被调用。
你可以修改你的代码,接受另一个回调函数,这样你的函数有机会处理查询结果,然后通知调用者:
```javascript
function fetchOrders(status, callback) {
con.query(`SELECT * FROM orders WHERE OStatus = '${status}';`, function(err, rows) {
if (err) {
callback(err);
}
var text = "";
if (rows.length < 1) {
text = "None";
} else {
for (i = 0; i < rows.length; i++) {
text = text + "`" + rows[i].OID.toString() + "`, ";
}
}
if (text === "") {
text = "None";
}
callback(null, text);
})
}
请注意,这些函数不再返回值,而是将值(或错误)传递给回调函数。这也需要更改调用此函数的代码,所以不再是:
var result = fetchOrders(status);
而是需要写成:
fetchOrders(status, function (err, result) {
if (err) {
throw err;
}
// 对结果进行处理
})
英文:
I assume you are not familiar with asynchronous calls in node. Calls that accept a callback are usually executed asynchronously, which means that the result of the call will not be available immediately (not even after the function call ended), but as soon as the result is available, the callback will be called.
You can modify your code to accept another callback, so that your function has a chance to do something with the query results, and then notify the caller:
function fetchOrders(status, callback){
con.query(`SELECT * FROM orders WHERE OStatus = '${status}';`, function(err,rows) {
if(err){
callback(err);
}
var text = "";
if(rows.length<1){
text = "None"
}else{
for(i=0;i<rows.length;i++){
text = text + "`" + rows[i].OID.toString() + "`, "
}
}
if(text===""){
text = "None"
}
callback(null, text);
})
}
Note that the functions don't return anymore, but pass the value (or error) to the callback. This requires changing also the code that calls this function, so that instead of:
var result = fetchOrders(status);
you need to write:
fetchOrders(status, function (err, result) {
if (err) {
throw err;
}
// do something with result
})
答案2
得分: 0
将这个函数转换成 promise 风格,就像下面这样:
function fetchOrders(status) {
return new Promise((resolve, reject) => {
con.query(`SELECT * FROM orders WHERE OStatus = '${status}';`, function (
err,
rows
) {
if (err) {
reject(err);
}
var text = "";
if (rows.length < 1) {
text = "None";
} else {
for (i = 0; i < rows.length; i++) {
text = text + '`' + rows[i].OID.toString() + '`, ';
}
}
if (text === "") {
text = "None";
}
resolve(text);
});
});
}
然后像这样调用它:
fetchOrders(status)
.then(result => {
console.log(result);
})
或者使用 async/await:
const response = await fetchOrders(status);
英文:
Convert this function to be promise style like this
function fetchOrders(status) {
return Promise((resolve, reject) => {
con.query(`SELECT * FROM orders WHERE OStatus = '${status}';`, function(
err,
rows
) {
if (err) {
reject(err);
}
var text = "";
if (rows.length < 1) {
text = "None";
} else {
for (i = 0; i < rows.length; i++) {
text = text + "`" + rows[i].OID.toString() + "`, ";
}
}
if (text === "") {
text = "None";
}
resolve(text);
});
});
}
and call it like this
fetchOrders
.then(result => {
console.log(result);
})
OR
const response = await fetchOrders();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论