简体   繁体   English

Javascript:将参数传递给回调函数

[英]Javascript: pass parameters to callback function

I want to call a function on the returned data from this database query, as in the snippet below. 我想在此数据库查询返回的数据上调用函数,如下面的代码片段所示。

function connectDB(query, data, callback) {
    pg.connect(process.env.DATABASE_URL, function (err, client) {
        if (err) {
            console.log(err);
        } else {
                client
                    .query(query, data, function (err, result) {
                        callback(result.rows);                            
                    });
        }
     }
}

function foo(data) {
  connectDB("SELECT * FROM mytable WHERE id=$1", someID, callbackfn(data));
}

I don't know how to write callbackfn so that I can use both data from the original function and rows from the db result. 我不知道如何编写callbackfn,这样我就可以同时使用原始函数中的数据和db结果中的行。

function callbackfn(data) {
  return function(rows) {
   // Do something with rows and data
  }
}

You don't accept any params with the callback function. 回调函数不接受任何参数。 How about: 怎么样:

function callback(rows) {
  /* not sure what your intention was here? nested functions? */
  return function () {
    return function () {
        // Do something with rows
    }
  }
}

And to be clear, can you post where you call the connectDB function? 明确地说,您可以在哪里发布调用connectDB函数的地方吗? As callback is name of the variable after it is passed to connectDB . 由于callback是传递给connectDB之后的变量的名称。

Why are you defining callback function? 为什么要定义回调函数? Please elaborate on this. 请对此进行详细说明。 Also, try calling connectDB like this: 另外,尝试像这样调用connectDB

 connectDB(query, data, function(rows) {
       // Do something with rows
 });

Read this answer on callbacks. 阅读有关回调的答案

You have an incredible level of nested functions. 您拥有令人难以置信的嵌套函数级别。 Moreover, you are defining callback as parameter, but it won't take the callback function you defined, if you are not calling the base function passing again "callback" as argument. 此外,您将回调定义为参数,但是,如果您不调用基函数,而又将“回调”作为参数,它将不会使用您定义的回调函数。 If you want to pass always the same callback, it doesn't make sense that you provide it as argument, you can directly call "callback" inside your "connectDB" function. 如果您希望始终传递相同的回调,那么将其作为参数提供就没有意义,可以直接在“ connectDB”函数内部调用“ callback”。 Moreover your callback returns a function, so it should be called again using again () . 此外,您的回调函数返回一个函数,因此应使用again ()再次调用它。 Then your main callback function needs to accept rows as parameter from outside. 然后,您的主要回调函数需要从外部接受行作为参数。 I kept your code using the callback parameter because I "hope" that the same name was just an example to explain that you want to provide every time a function in order to manipulate as you want the rows. 我使用callback参数保留您的代码,因为我“希望”同一个名字只是一个示例,以说明您希望每次提供一个函数来按需操作行。 You code should look like this: 您的代码应如下所示:

function connectDB(query, data, callback) {
    pg.connect(process.env.DATABASE_URL, function (err, client) {
        if (err) {
            console.log(err);
        } else {
                client
                    .query(query, data, function (err, result) {
                        // this returns a function so it needs to be called again
                        callback(result.rows)();                            
                    });
        }
     }
}

// third inner function was useless
function callback(rows) {
  return function () {
    // Do something with rows
  }
}

// the third parameter HAS TO BE callback, otherwise you will not pass the function you defined. 
connectDB(query, data, callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM