Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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/2/node.js/42.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.get()导致TypeError:无法读取属性';获取';空的_Javascript_Node.js_Sqlite - Fatal编程技术网

Javascript sqlite.get()导致TypeError:无法读取属性';获取';空的

Javascript sqlite.get()导致TypeError:无法读取属性';获取';空的,javascript,node.js,sqlite,Javascript,Node.js,Sqlite,当尝试执行sql.get()调用时,它在某些地方有效,但在其他地方无效,我似乎不知道有什么区别。 数据库已成功打开,或者至少我从未遇到错误,也从未关闭过。我还在第二个示例中使用Discord.js(工作示例),但不确定这是否相关。 在本例中,它不起作用。 test.js: 堆栈跟踪: $ node test.js TypeError: Cannot read property 'get' of null at Promise (/Users/julianveerkamp/node_mo

当尝试执行sql.get()调用时,它在某些地方有效,但在其他地方无效,我似乎不知道有什么区别。
数据库已成功打开,或者至少我从未遇到错误,也从未关闭过。我还在第二个示例中使用Discord.js(工作示例),但不确定这是否相关。
在本例中,它不起作用。
test.js:

堆栈跟踪:

$ node test.js 
TypeError: Cannot read property 'get' of null
    at Promise (/Users/julianveerkamp/node_modules/sqlite/main.js:225:18)
    at Promise (<anonymous>)
    at Database.get (/Users/julianveerkamp/node_modules/sqlite/main.js:224:12)
    at /Users/julianveerkamp/workspace/test-bot/test.js:6:13
    at Promise (<anonymous>)
    at Object.exports.getItemID (/Users/julianveerkamp/workspace/test-bot/test.js:5:12)
    at Object.<anonymous> (/Users/julianveerkamp/workspace/test-bot/test.js:15:9)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
它很好用

command.js:

const sql = require("sqlite");
sql.open("./data.sqlite");

exports.run = (client, message, args) => {
    sql.get(`SELECT * FROM scores WHERE userID ="${message.author.id}"`).then(row => {
        // Do stuff with row
    }).catch(() => {
        console.error;
    });
}

这可能是sqlite中的一个bug,所以我继续使用了
sqlite3
,并用util.promisify()包装了函数


注意:函数中的新承诺不是必需的。可以从get函数返回承诺

这不是问题,但仅供参考,该代码正沦为“新承诺反模式”的牺牲品。
getItemID
没有理由创建新承诺,它已经有一个承诺链(来自
get
)。运行应用程序时,代码中是否存在行
//Do stuff with row
?stacktrace的
/Users/user/workspace/test bot/test.js:8:13
行看起来可疑!啊,谢谢你的提示。我必须阅读更多关于“新承诺反模式”和一般承诺的内容,因为我对node.js和javascript相当陌生。@未定义是指Object.exports.getItemID的
//do stuff with row
只是检查该行是否存在,以及是否存在
让itemID=row.itemID
,但即使所有这些都被
console.log(
do stuff
替换,也会失败。我将用实际的runnable和正确的stacktrace更新帖子。抛出错误的是
。/node\u modules/sqlite/main.js
文件的第225行。我没有使用过那个库,所以很不幸,我不知道那个文件中发生了什么。尝试执行不同的SQL查询并检查发生了什么。
const sql = require("sqlite");
sql.open("./data.sqlite");
// other stuff
let commandFile = require(`./commands/command.js`);
commandFile.run(client, message, args);
// other stuff
const sql = require("sqlite");
sql.open("./data.sqlite");

exports.run = (client, message, args) => {
    sql.get(`SELECT * FROM scores WHERE userID ="${message.author.id}"`).then(row => {
        // Do stuff with row
    }).catch(() => {
        console.error;
    });
}
const util = require('util');
const sqlite3 = require('sqlite3');

var db = new sqlite3.Database('./data.sqlite');

db.get = util.promisify(db.get);
db.run = util.promisify(db.run);

exports.getItemID = function(item) {
    return new Promise(function (fulfill, reject){
        db.get(`SELECT * FROM items WHERE name="${item}"`).then(row => {
            console.log(`do stuff`);
        }).catch((e) => {
            console.error;
            reject(e);
        });
    });
}

exports.getItemID("Coins").then(itemID => {
    console.log(itemID);
}).catch((e) => {
    console.log(e);
});