Sqlite .all() 函数 returns 一个未定义的承诺。我怎样才能进一步使用结果?
Sqlite .all() function returns an undefined promise. How can I use the result further?
我刚开始使用 JS 和 SQLite。对于我的具体问题找不到任何帮助。
我想保留一个 return 以在一秒钟内将其用作外键 table。
这是我的功能:
async function getIdbyName(table, row, name) {
let nameNeu = '"' + name + '"';
let sql =
"SELECT id as print FROM " +
table +
" WHERE " +
row +
" = " +
nameNeu +
" LIMIT 1;";
// await db.get(sql, (err, row) => {
// console.log(row.print);
// return row;
// });
return await db.get(sql);
}
getIdbyName("...", "...", "...")
.then(function (value) {
console.log("Success!", value); // <-- prints: Success! undefined
})
.catch(function (err) {
console.log("Caught an error!", err);
});
console.log(getIdbyName("r_Tag", "r_Tag", "test")); //<-- shows me a Promise
我必须做什么才能使 promise 不会在函数之外保持未定义状态?
其余代码:
var sqlite3 = require("sqlite3").verbose();
let db = new sqlite3.Database("./assets/db/test.db", (err) => {
if (err) {
return console.error(err.message);
}
console.log("Connected to the SQlite database.");
});
我的其他函数只是创建一些字符串,我 运行 几次 db.run(...) 添加一些 tables。
更简单地说,您的 getIdByName
函数从来没有 return 任何东西。您需要 return 从 await db.get(...)
返回的值。一旦你这样做,当你调用 getIdByName
时,你应该从数据库中得到你的响应。
您还应该知道您的代码容易受到 SQL injection, which is a major security vulnerability. Instead of concatenating a string, you should use a .
的影响
async function getIdbyName(table, row, name) {
return await db.get(sql);
}
更新:SQLlite 的 Promise 包装器 - 2020 年 8 月 1 日
基于this blog post, it seems it's not possible to do native async/await using sqlite3. However, you can write a wrapper function around db.all
to return a promise, which will allow you to use async/await. Note the use of ?
in the SQL statement, which will be replaced by the values of the array in the second argument following the same order. For more help with parameterized queries, read the params bullet point in the documentation here.
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./assets/db/test.db", (err) => {
if (err) {
return console.error(err.message);
}
console.log("Connected to the SQlite database.");
});
db.query = function (sql, params = []) {
const that = this;
return new Promise(function (resolve, reject) {
that.all(sql, params, function (error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
};
async function getIdByName(table, name) {
// assemble sql statement
const sql = `
SELECT id
FROM ?
WHERE name = ?;
`;
return await db.query(sql, [table, name]);
}
// need async to call
(async () => {
const result = await getIdByName('books', 'my_name');
console.log(result);
})();
我刚开始使用 JS 和 SQLite。对于我的具体问题找不到任何帮助。
我想保留一个 return 以在一秒钟内将其用作外键 table。 这是我的功能:
async function getIdbyName(table, row, name) {
let nameNeu = '"' + name + '"';
let sql =
"SELECT id as print FROM " +
table +
" WHERE " +
row +
" = " +
nameNeu +
" LIMIT 1;";
// await db.get(sql, (err, row) => {
// console.log(row.print);
// return row;
// });
return await db.get(sql);
}
getIdbyName("...", "...", "...")
.then(function (value) {
console.log("Success!", value); // <-- prints: Success! undefined
})
.catch(function (err) {
console.log("Caught an error!", err);
});
console.log(getIdbyName("r_Tag", "r_Tag", "test")); //<-- shows me a Promise
我必须做什么才能使 promise 不会在函数之外保持未定义状态?
其余代码:
var sqlite3 = require("sqlite3").verbose();
let db = new sqlite3.Database("./assets/db/test.db", (err) => {
if (err) {
return console.error(err.message);
}
console.log("Connected to the SQlite database.");
});
我的其他函数只是创建一些字符串,我 运行 几次 db.run(...) 添加一些 tables。
更简单地说,您的 getIdByName
函数从来没有 return 任何东西。您需要 return 从 await db.get(...)
返回的值。一旦你这样做,当你调用 getIdByName
时,你应该从数据库中得到你的响应。
您还应该知道您的代码容易受到 SQL injection, which is a major security vulnerability. Instead of concatenating a string, you should use a
async function getIdbyName(table, row, name) {
return await db.get(sql);
}
更新:SQLlite 的 Promise 包装器 - 2020 年 8 月 1 日
基于this blog post, it seems it's not possible to do native async/await using sqlite3. However, you can write a wrapper function around db.all
to return a promise, which will allow you to use async/await. Note the use of ?
in the SQL statement, which will be replaced by the values of the array in the second argument following the same order. For more help with parameterized queries, read the params bullet point in the documentation here.
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./assets/db/test.db", (err) => {
if (err) {
return console.error(err.message);
}
console.log("Connected to the SQlite database.");
});
db.query = function (sql, params = []) {
const that = this;
return new Promise(function (resolve, reject) {
that.all(sql, params, function (error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
};
async function getIdByName(table, name) {
// assemble sql statement
const sql = `
SELECT id
FROM ?
WHERE name = ?;
`;
return await db.query(sql, [table, name]);
}
// need async to call
(async () => {
const result = await getIdByName('books', 'my_name');
console.log(result);
})();