简体   繁体   English

如何使变量可用于javascript中的回调函数

[英]How to make a variable available to a callback function in javascript

In the following code, how do I make the variable songlist available to the callback function complete ? 在下面的代码中,如何使可用于回调函数的变量songlist complete

If I run the code as is, sqlite successfully populates the songlist , but when the callback is run the variable becomes null . 如果我按原样运行代码,则sqlite成功填充了songlist ,但运行回调时,变量变为null

Basically, I'm trying to figure out a way to ensure sqlite completes running before console.log(songlist) runs. 基本上,我试图找到一种方法来确保sqlite在console.log(songlist)运行之前完成运行。 I thought using a callback would do it, but then run into this variable passing issue. 我以为使用回调会做到这一点,但随后遇到了该变量传递问题。 Thanks. 谢谢。

    var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database('mydb.db');

    var songlist = []
    var complete = function (songlist) {
        console.log(songlist);
    }
    db.serialize(function () {
        db.each("SELECT rowid AS id, info FROM lorem where info like '%" + song + "%'", function (err, row) {
            songlist.push(row.info);
            console.log("each")
        }, complete)

    })

In your current code songlist is global, which means it's already available to your callback. 在您当前的代码中, songlist是全局的,这意味着您的回调已经可以使用它。 When you add the songlist argument to the callback you are shadowing it which makes the global no longer visible. 当您将songlist参数添加到回调中时,您将对其进行阴影处理,这将使全局不再可见。

You could simple say: 您可以简单地说:

var complete = function () {
    console.log(songlist);
}

and log the global object. 并记录全局对象。

But that's not very satisfying because you should generally avoid globals when not absolutely required. 但这不是很令人满意,因为在并非绝对必要时,通常应避免使用全局变量。

A better idea is to define songlist in the function and pass it to your callback when you're done: 一个更好的主意是在函数中定义songlist ,并在完成songlist其传递给回调:

var complete = function (songlist) {
    console.log(songlist);
}
db.serialize(function () {
    var songlist = []

    db.each("SELECT rowid AS id, info FROM lorem where info like '%" + song + "%'", function (err, row) {
        songlist.push(row.info);
        console.log("each")
    }, function(){
          complete(songlist)
       }
    })
)}

Of course you don't really need a separate function — you could just log the songfest directly in the callback to your db function. 当然,您实际上并不需要一个单独的函数-您只需将Songfest直接记录在db函数的回调中即可。

By creating an anonymous function that passes the songlist to complete as shown bellow: 通过创建一个匿名函数来传递歌曲列表,如下所示:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb.db');

var songlist = []
var complete = function (songlist) {
    console.log(songlist);
}
db.serialize(function () {
    db.each("SELECT rowid AS id, info FROM lorem where info like '%" + song + "%'", function (err, row) {
        songlist.push(row.info);
        console.log("each")
    }, function() { complete(songlist) })

})

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

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