Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从SQLite中提取数据_Javascript_Database_Sqlite_Nonblocking_Web Sql - Fatal编程技术网

Javascript 从SQLite中提取数据

Javascript 从SQLite中提取数据,javascript,database,sqlite,nonblocking,web-sql,Javascript,Database,Sqlite,Nonblocking,Web Sql,我有个问题,不知道怎么解决 我正在开发一个小型web应用程序。在应用程序中有一个过帐系统 用户可以在这里发布文章。现在,当用户登录时,我保存 他的电子邮件(对每个用户来说都是唯一的)在SQLite数据库中,当用户添加帖子时,我将“帖子表单”中的所有数据和他的电子邮件发送到服务器(然后我将其保存在mongoDB中)。问题是电子邮件总是未定义的。以下是守则的相关部分: 从数据库中提取数据 currentUserName和currentUserEmail是全局变量 验证和发布表单 问题是: 控制台日志

我有个问题,不知道怎么解决

我正在开发一个小型web应用程序。在应用程序中有一个过帐系统 用户可以在这里发布文章。现在,当用户登录时,我保存 他的电子邮件(对每个用户来说都是唯一的)在SQLite数据库中,当用户添加帖子时,我将“帖子表单”中的所有数据和他的电子邮件发送到服务器(然后我将其保存在mongoDB中)。问题是电子邮件总是未定义的。以下是守则的相关部分:

从数据库中提取数据 currentUserName和currentUserEmail是全局变量

验证和发布表单 问题是:

控制台日志和发送到服务器的数据总是未定义的,因为它发生在从SQLite检索数据的回调函数结束之前

我应该如何重构我的代码,以便从数据库中提取的数据将结束,然后才将数据发送到服务器?
我查找了这个问题,但找不到答案。

这是JavaScript中的一个常见问题,因为它是同步的。建议的解决方案是将回调传递到异步方法中,并在成功时触发该回调

所以看看你的代码,你可以把它修改成

function extractDetailsFromDB(callback) {
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM currentUser WHERE id=1', [], function(tx, results) {
            var len = results.rows.length;
            currentUserName = results.rows.item(0).name;
            currentUserEmail = results.rows.item(0).data;
            callback();
        }, onError);
    }, errorCb);
}
...
extractDetailsFromDb(function() {
    console.log(currentUserName + ' ' + currentUserEmail);  // This is undefined
    var newPostJson = { writer: currentUserEmail, title: title.value, artist: artist.value,            
                       albumOrSong: albumSong.value, content: content.value, genre: 
                       genre.value};        

    $.post(URLAddress + '/addPost', newPostJson).done(function(data) {});
    window.location = '#index';
});

谢谢你!!!!那很有魅力!顺便说一句,整个回调函数实际上有点酷。对我来说有点奇怪,但很酷:)@elmekiesIsrael V8引擎针对吞吐量进行了优化,因此它不鼓励阻塞执行或等待方法完成,使用回调是解决该问题的更自然的方法。
function validateNewPost() {
    // Here I validate the data from the form... works well.

    if(error_appear === "false") {
        extractDetalisFromDB();
        console.log(currentUserName + ' ' + currentUserEmail);  // This is undefined
        var newPostJson = { writer: currentUserEmail, title: title.value, artist: artist.value,            
                           albumOrSong: albumSong.value, content: content.value, genre: 
                           genre.value};        

        $.post(URLAddress + '/addPost', newPostJson).done(function(data) {});
        window.location = '#index';
    }
}
function extractDetailsFromDB(callback) {
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM currentUser WHERE id=1', [], function(tx, results) {
            var len = results.rows.length;
            currentUserName = results.rows.item(0).name;
            currentUserEmail = results.rows.item(0).data;
            callback();
        }, onError);
    }, errorCb);
}
...
extractDetailsFromDb(function() {
    console.log(currentUserName + ' ' + currentUserEmail);  // This is undefined
    var newPostJson = { writer: currentUserEmail, title: title.value, artist: artist.value,            
                       albumOrSong: albumSong.value, content: content.value, genre: 
                       genre.value};        

    $.post(URLAddress + '/addPost', newPostJson).done(function(data) {});
    window.location = '#index';
});