{ let q = await ctx.db .execute(`SELECT w.create_time -6ren">
gpt4 book ai didi

node.js - 如何解决Promise { } 的问题?

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:02 24 4
gpt4 key购买 nike

这是我的代码:

router.get("test", async ctx => {
let q = await ctx.db
.execute(`SELECT w.create_time as create_time,w.camera_id,w.status,
w.problem_type_id,w.repair_user_id,w.no as no,wp.pic_url
FROM work_flow w
left OUTER
JOIN work_flow_pic wp ON w.no=wp.flow_id`);
let data = q.map(item => {
let url = ctx.db.execute(
`select * from work_flow_pic where flow_id=${item.no}`
);
return {
create_time: item.create_time,
no: item.no,
url: url.then(function(result) {
result;
})
};
});
console.log(data);
let retstr = `[{"ret":"0"}]`;
await ctx.render("interface_ret", {
retstr
});
});

当我运行代码时,我得到以下输出:

    [ { create_time: 2018-10-09T02:47:54.000Z,
no: '153905327352986',
url: Promise { <pending> } },
{ create_time: 2018-10-09T02:47:54.000Z,
no: '153905327352986',
url: Promise { <pending> } },
{ create_time: 2018-10-09T08:51:33.000Z,
no: '153905327352987',
url: Promise { <pending> } } ]

不知道怎么解决

最佳答案

当您想要等待 Promise 解析并获取其返回值时,您可以像上面那样使用 await :

let q = await ctx.db.execute(`SELECT w.create_time as 
create_time,w.camera_id,w.status,
w.problem_type_id,w.repair_user_id,w.no as no,wp.pic_url
FROM work_flow w left OUTER JOIN work_flow_pic wp ON w.no=wp.flow_id`);//get q from table
let data = q.map(item => {
let url = await ctx.db.execute(`select * from work_flow_pic where
flow_id=${item.no}`); //get url from q
return {
create_time: item.create_time,
no: item.no,
url: url
})
}
});
console.log(data);

关于node.js - 如何解决Promise { <pending> } 的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724256/

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