Nodejs等待mysql2结果,然后继续执行

问题描述

我是 nodejs 新手,想玩玩 MysqL 数据库

我有这样的代码

模型/Db_sys_bot.js

const MysqL = require('MysqL2/promise');

module.exports = {
    async Get_ByBotId(botId) {
        // create the connection
        const connection = await MysqL.createConnection({
            host:'dbhost',user: 'dbuser',password: 'dbpass',database: 'dbname'
        });
        
        // query database
        const [rows,fields] = await connection.execute('SELECT * FROM sys_bot WHERE botId = ?',[botId]);

        return rows;
    },};

main.js

const Db_sys_bot = require("./models/Db_sys_bot");

//Bunch of code

let arrBotData = Db_sys_bot.Get_ByBotId("kokoro");
console.log(arrBotData);
if (arrBotData.length < 1)
    throw "No bot data available in system";

//Bunch of code

所以,在 index.js 中,如果我没有机器人,我想检查数据库,然后处理结束。但是当我尝试时,arrBotData 返回承诺对象而不是数据库数据。如何让进程等待查询完成?

我有很多案例使用数据库进行验证,所以如果我使用回调,我认为嵌套回调太多。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)