【发布时间】:2017-08-06 16:57:23
【问题描述】:
NodeJS 6.9.3
我之前的做法是这样的:
一个名为“get_user()”的外部函数:
return database_queries.get_user(user_name)
.then(function(results_from_database) {
然后该函数使用 Knex 运行数据库调用,并返回:
var dbquery = Multiline.stripIndent(function () {/*
SELECT
u.id as profile_id,
'user' as type_of_profile
FROM
user_profile u
WHERE name REGEXP "[[:<:]]||user_name||[[:>:]]"
*/});
dbquery = dbquery.replaceAll('||user_name||', user_name);
return DB.knex.raw(dbquery).then(function(result1) {
for(var index_of_results = 0; index_of_results < result1[0].length; index_of_results++) {
var document1 = result1[0][index_of_results];
array_of_maps_with_profile_type_and_profile_id[document1["type_of_profile"]].push(document1["profile_id"]);
}
当我这样做时,数据库查询运行并获取数据,但这是异步发生的,结果从未返回到外部函数。换句话说,外部函数在数据库查询运行之前很久就完成了。
所以我尝试将内部函数包装在 Promise 中:
function get_user(user_name) {
return new Promise(function(resolve, reject) {
resolve ()
.then(function() {
var dbquery = Multiline.stripIndent(function () {/*
SELECT
u.id as profile_id,
'user' as type_of_profile
FROM
user_profile u
WHERE name REGEXP "[[:<:]]||user_name||[[:>:]]"
*/});
dbquery = dbquery.replaceAll('||user_name||', user_name);
return DB.knex.raw(dbquery).then(function(result1) {
for(var index_of_results = 0; index_of_results < result1[0].length; index_of_results++) {
var document1 = result1[0][index_of_results];
array_of_maps_with_profile_type_and_profile_id[document1["type_of_profile"]].push(document1["profile_id"]);
}
现在似乎从未调用过数据库调用。当它们运行时,它们会出现在日志中,但现在日志中不会出现任何数据库查询。看起来这个内部函数现在返回了一个 Promise,但从未调用过 Promise 的“resolve()”部分。
我在这里做错了什么?
【问题讨论】:
-
knex.raw返回一个承诺,所以你不需要在这里创建Promise。 -
然后数据库查询在后台运行,而不是返回给调用数据库函数的函数。正在从循环中调用数据库函数。
-
在node.js中没有
background,这个函数是异步的。这意味着它不会立即返回结果。 -
@alexmac -- 是的,我知道。我不清楚你想说什么。
-
@alexmac -- 我希望在调用代码的 .then() 块中捕获结果。