gpt4 book ai didi

javascript - 使用 knex js 时如何返回值?

转载 作者:行者123 更新时间:2023-11-30 19:15:44 24 4
gpt4 key购买 nike

我有一个函数试图让它使用 return 从数据库返回一个 knex 结果,但它似乎不起作用?我正在使用 knex 库而不是 ajax。

代码

//function

function runKnex(){
//Run Queries and send Content
var result = knex.select().table('User')
return result.then(function(rows){
return rows;
})
}

//calling function

mainWindow.webContents.on('did-finish-load',()=>{
const knexres = runKnex();
console.log(knexres);
});

结果

  _bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined
}

为什么不是数组?我如何让它返回一个数组?

NB when you console.log(rows) you get:

[
{ UserId: 1, FirstName: 'Tarik', LastName: 'Guney', Age: 30 },
{ UserId: 2, FirstName: 'Sumeyye', LastName: 'Guney', Age: 29 }
]

最佳答案

// 1. with async await
function runKnex(){
//Run Queries and send Content
var result = knex.select().table('User')
return result.then(function(rows){
return rows;
})
}

//calling function
mainWindow.webContents.on('did-finish-load',async ()=>{
const knexres = await runKnex();
console.log(knexres);
});

// 2.

function runKnex(){
//Run Queries and send Content

return knex.select().table('User').then()

}

//calling function
mainWindow.webContents.on('did-finish-load', ()=>{
runKnex().then(knexres=>{

console.log(knexres);

});
});

关于javascript - 使用 knex js 时如何返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58028405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com