如果我 return 我从函数查询的结果使用 pg 时,它 return 未定义,但如果我控制台记录它,它会打印出来怎么办?
How come when using pg if i return the result of my query from a function, it returns undefined, but if i console log it, it prints?
这是我的代码:
const queryFirstNames = function (qString) {
let firstNameMatches;
client.query('SELECT * FROM famous_people WHERE first_name = ', [qString], (err, result) => {
if (err) {
return console.error('error running query', err);
}
firstNameMatches = result.rows;
return firstNameMatches;
client.end();
});
};
console.log(queryFirstNames(qString));
});
This code return undefined and doesn't end the connection with the database
但是如果我在函数内部 console.log firstNameMatches 而不是 returning 然后只调用函数而不进行控制台日志记录,I get the result I want, and the database connection closes properly.
我想做的是 return 这个查询的结果并在另一个函数中使用它,所以我不希望它控制台记录结果,但是当我尝试 return 结果,它给我未定义的,也没有关闭数据库连接。
我认为您遇到的问题是,当您 returning firstNameMatches 时,您 return 只是为了 client.query 的回调。 firstNameMatches 是在 queryFirstNames 函数的上下文中设置的,但是您永远不会 return 返回。也就是说,您还利用了对 client.query 的异步调用。这意味着当您从 queryFirstNames 函数 return 时,您很可能还没有完成查询。据此,我很确定您想利用承诺来管理这个或其他答案之一的事件。
除此之外,您还需要将 return 移到 client.end 之前。为了玩弄我的答案,我创建了一个您可以看到的 jsfiddle here。我不得不通过 Promise 创建自己的承诺,只是为了模拟执行查询的客户端。
const queryFirstNames = function (qString) {
let firstNameMatches;
client.query('SELECT * FROM famous_people WHERE first_name = ', [qString])
.then((result) => {
firstNameMatches = result.rows;
client.end();
return firstNameMatches;
})
.then(f => {
console.log(f);
})
.catch(e => {
if (err) {
return console.error('error running query', err);
}
});
};
您甚至在执行 client.end() 之前就返回了调用;这样您的客户端连接将永远不会结束。你可以这样做:
const query = client.query(
'SELECT * FROM famous_people WHERE first_name = ',
[qString],
(err, result) => {
if (err) {
return console.error('error running query', err);
}
});
query.on('row', (row) => {
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', () => {
return res.json(results);
});
这是我的代码:
const queryFirstNames = function (qString) {
let firstNameMatches;
client.query('SELECT * FROM famous_people WHERE first_name = ', [qString], (err, result) => {
if (err) {
return console.error('error running query', err);
}
firstNameMatches = result.rows;
return firstNameMatches;
client.end();
});
};
console.log(queryFirstNames(qString));
});
This code return undefined and doesn't end the connection with the database
但是如果我在函数内部 console.log firstNameMatches 而不是 returning 然后只调用函数而不进行控制台日志记录,I get the result I want, and the database connection closes properly.
我想做的是 return 这个查询的结果并在另一个函数中使用它,所以我不希望它控制台记录结果,但是当我尝试 return 结果,它给我未定义的,也没有关闭数据库连接。
我认为您遇到的问题是,当您 returning firstNameMatches 时,您 return 只是为了 client.query 的回调。 firstNameMatches 是在 queryFirstNames 函数的上下文中设置的,但是您永远不会 return 返回。也就是说,您还利用了对 client.query 的异步调用。这意味着当您从 queryFirstNames 函数 return 时,您很可能还没有完成查询。据此,我很确定您想利用承诺来管理这个或其他答案之一的事件。
除此之外,您还需要将 return 移到 client.end 之前。为了玩弄我的答案,我创建了一个您可以看到的 jsfiddle here。我不得不通过 Promise 创建自己的承诺,只是为了模拟执行查询的客户端。
const queryFirstNames = function (qString) {
let firstNameMatches;
client.query('SELECT * FROM famous_people WHERE first_name = ', [qString])
.then((result) => {
firstNameMatches = result.rows;
client.end();
return firstNameMatches;
})
.then(f => {
console.log(f);
})
.catch(e => {
if (err) {
return console.error('error running query', err);
}
});
};
您甚至在执行 client.end() 之前就返回了调用;这样您的客户端连接将永远不会结束。你可以这样做:
const query = client.query(
'SELECT * FROM famous_people WHERE first_name = ',
[qString],
(err, result) => {
if (err) {
return console.error('error running query', err);
}
});
query.on('row', (row) => {
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', () => {
return res.json(results);
});