sqlite.get() 导致 TypeError:无法读取 null 的属性“get" [英] sqlite.get() results in TypeError: Cannot read property 'get' of null
问题描述
当尝试执行 sql.get() 调用时,它在某些地方有效,但在其他地方无效,我似乎无法弄清楚有什么区别.
数据库已成功打开,或者至少我从未收到错误并且从未关闭过.我还在第二个(工作示例)中使用了 Discord.js,但不确定这是否相关.
在这个例子中它不起作用.
测试.js:
When trying to execute a sql.get() call it works in some places, but not in others and I can't seem to figure out what's the difference.
The database is sucessfully opened or at least I never got an error and it never gets closed. I also use Discord.js in the second (working example), not sure if that is relevant, though.
In this example it doesn't work.
test.js:
const sql = require("sqlite");
sql.open("./data.sqlite");
exports.getItemID = function(item) {
return new Promise(function (fulfill, reject){
sql.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);
});
堆栈跟踪:
$ 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)
但是当我用
const sql = require("sqlite");
sql.open("./data.sqlite");
// other stuff
let commandFile = require(`./commands/command.js`);
commandFile.run(client, message, args);
// other stuff
它工作得很好.
command.js:
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
中的一个错误,所以我继续使用 sqlite3
代替并包装/promisified自己使用 util.promisify() 函数.
This might be a bug in sqlite
so I went ahead and used sqlite3
instead and wrapped/promisified the functions myself with util.promisify().
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);
});
注意:函数内部的新 Promise 不是必须的.人们可以从 get 函数返回承诺.
Note: The new Promise inside the function is not necessary. One could just return the promise from the get function.
这篇关于sqlite.get() 导致 TypeError:无法读取 null 的属性“get"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!