1

我正在尝试执行 mysql 查询,将值异步分配给 const 以供以后在我的代码中使用。

到目前为止,我所拥有的是:

const plan_free = await con1.promise().query("SELECT stripe_price1 FROM funzioni WHERE titolo='SmartBanca'")
.then( ([rows,fields]) => {
    return rows[0]['stripe_price1'];
})
.catch(console.log)
.then( () => con1.end());
console.log("Plan_free: "+ plan_free);

但 plan_free 返回 [object Object]。我确信我已经接近目标,但我不明白我仍然做错了什么。

编辑:通过添加以下行:

plan_free1 = JSON.stringify(plan_free);
console.log("Plan_free: "+ plan_free1);

我看到 plan_free 的以下内容:

Plan_free: {"_events":{},"_eventsCount":0,"next":null}

这实际上甚至不接近我想要实现的目标。

4

1 回答 1

0

好的,我终于成功了。

var plain_free='';
await con1.promise().query("SELECT stripe_price1 FROM funzioni WHERE titolo='SmartBanca'")
.then( ([rows,fields]) => {
    plan_free = rows[0]['stripe_price1'];
    return plan_free;
})
.catch(console.log)
.then( () => con1.end());
            
console.log("Plan_free: "+ plan_free);

这样我就能够从数据库中获取值并将其分配给节点中的变量。这将稍后在我的代码中使用,而无需将其嵌套在查询的回调中。

于 2021-01-04T16:23:08.767 回答