[英]Returning the result of a node-postgres query
I am attempting to return the result of a node-postgres query and store it in a variable.我正在尝试返回 node-postgres 查询的结果并将其存储在一个变量中。 I can manage a console.log just fine, but cannot find a way to return the result so that it is accessible outside of the query method.
我可以很好地管理 console.log,但找不到返回结果的方法,以便在查询方法之外可以访问它。 I'm at a loss, and know I must be missing something obvious (or multiple obvious things), because if it isn't possible to do this I don't understand the point of node-postgres since all I will ever be able to do is log my results to the console.
我不知所措,并且知道我必须遗漏一些明显的东西(或多个明显的东西),因为如果不可能做到这一点,我就不明白 node-postgres 的意义,因为我将永远能够要做的是将我的结果记录到控制台。
I have tried the code below, along with a version using promises, and both receive the same result, 'undefined.'我已经尝试了下面的代码以及使用 Promise 的版本,并且都收到了相同的结果“未定义”。 A console.log within the else works fine, but a return does not make the result accessible to the rest of the function.
else 中的 console.log 工作正常,但返回不会使 function 的 rest 可以访问结果。 In the code below, my return comes back as 'undefined' as would a console.log in its place.
在下面的代码中,我的返回返回为“未定义”,console.log 代替它。
var selectFrom = function(data, table, condition) {
var queryResult;
pool.query(`SELECT ${data} FROM ${table} ${condition}`, function(err, result) {
if(err) { console.log(err); }
else { queryResult = result.rows[0][data]; }
})
pool.end();
return queryResult;
}
var result = selectFrom('amount','total_nonfarm_monthly_sa', `WHERE month='2019-08-31'`);
console.log(result);
This is what you are looking for:这就是您要查找的内容:
async function selectFrom(data, table, condition) {
try {
const res = await pool.query(
`SELECT ${data} FROM ${table} ${condition}`
);
return res.rows[0][data];
} catch (err) {
return err.stack;
}
}
Outside of the previous function:在之前的 function 之外:
async function whateverFuncName () {
var result = await selectFrom('amount','total_nonfarm_monthly_sa', `WHERE month='2019-08-31'`);
console.log(result);
}
EDIT: I didn't run this code snippet but the main concept is pretty straightforward.编辑:我没有运行此代码片段,但主要概念非常简单。
The "query" method is an async call, you should use async/await or Promise. “查询”方法是异步调用,您应该使用 async/await 或 Promise。
With async/await:使用异步/等待:
await client.connect()
const res = await client.query("SELECT amount FROM total_nonfarm_monthly_sa WHERE month='2019-08-31'");
console.log(res.rows[0]);
await client.end();
Edit: I can see that there's an option of callback, but I would use the async/await编辑:我可以看到有一个回调选项,但我会使用 async/await
You need to use callbacks:您需要使用回调:
var selectFrom = function(data, table, condition, callback) {
pool.query(`SELECT ${data} FROM ${table} ${condition}`, function(err, result) {
if(err)
return callback(err);
callback(null, result.rows[0][data]);
})
pool.end();
}
selectFrom('amount','total_nonfarm_monthly_sa', `WHERE month='2019-08-31'`, function(err, result){
console.log(err, result);
});
or promises:或承诺:
var selectFrom = function(data, table, condition) {
return new Promise(function(resolve, reject){
pool.query(`SELECT ${data} FROM ${table} ${condition}`, function(err, result) {
if(err)
return reject(err);
resolve(result.rows[0][data]);
})
pool.end();
});
}
selectFrom('amount','total_nonfarm_monthly_sa', `WHERE month='2019-08-31'`)
.then(function(result){
console.log(result);
}).catch(function(err){
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.