[英]How can an async Knex query be run at a debugger statement's execution halt?
如果我在觸發debugger
語句之前硬編碼並await
Knex 查詢語句到代碼和console.log()
中,我會得到預期的 output。 但是,在點擊debugger
語句后,我希望能夠執行一些額外的 Knex 查詢,但我要么沒有收到預期的響應,要么收到錯誤。 例如,以下是我在調試器語句中暫停時在控制台中嘗試的一些語句,以及輸出:
knex("users").where({username: "raj"})
Builder {client: Client_PG, and: Builder, _single: Object, _statements: Array(1), _method: "select", ...}
console.log(knex("users").where({username: "raj"}))
Builder {client: Client_PG, and: Builder, _single: Object, _statements: Array(1), _method: "select", ...}
await knex("users").where({username: "raj"})
SyntaxError: await 僅在異步中有效 function
await console.log(knex("users").where({username: "raj"}))
SyntaxError: await 僅在異步中有效 function
console.log(await knex("users").where({username: "raj"}))
SyntaxError: missing ) 在參數列表之后
我正在使用 VSCode,因此我也嘗試多次按下Step Over和Step Into按鈕(直到整個堆棧結束),但我從未在控制台中收到預期的 output。
這樣做的正確方法是什么?
您看到的錯誤是因為應該在async
function 中調用每個await
。
我不確定您在哪里嘗試執行查詢,但您可以嘗試頂級等待語法:
const executeQuery = async () => {
try {
const result= await knex("users").where({username: "raj"})
console.log({result})
} catch (error) {
console.log({error})
}
})
executeQuery()
或者同樣,使用中間執行的 function語法(在我看來很難閱讀):
(async () => {
try {
const result= await knex("users").where({username: "raj"})
console.log({result})
} catch (error) {
console.log({error})
}
})()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.