Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步方法在调用另一个方法后没有显示任何进度_Javascript_Mysql_Node.js_Async Await_Mqtt - Fatal编程技术网

Javascript 异步方法在调用另一个方法后没有显示任何进度

Javascript 异步方法在调用另一个方法后没有显示任何进度,javascript,mysql,node.js,async-await,mqtt,Javascript,Mysql,Node.js,Async Await,Mqtt,我有一个节点jsdb.js类,它返回一个数据数组 //db.js const mysql = require('mysql'); var subscribed = []; const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'mydb' }); connection.connect((err) => { if (err)

我有一个
节点js
db.js
类,它返回一个数据数组

//db.js
const mysql = require('mysql');
var subscribed = [];

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'mydb'
});
connection.connect((err) => {
  if (err) throw err;
  console.log('DB Connected!');
});


async function getTopics(subName) {

var qry = `SELECT topic_name from table where topic_name='${subName}'`;

await connection.query(qry, (err,rows) => {
  if(err) throw err;  
  rows.forEach( (row) => {
      subscribed.push(row.topic_name);
      return subscribed;
    });
});
}
module.exports.getTopics = getTopics; 
在我的另一个类中,我在下面的代码中使用了
mqtt
,请参见其中的注释

//sub.js
var mqtt = require('mqtt')
var Broker_URL = 'mqtt://test.mosquitto.org';
var client  = mqtt.connect(Broker_URL)
const db = require('./db');

const subName = 'iPhoneX';
var subscribed = [];

async function getTopics(){
  subscribed = await db.getTopics(subName);
}

client.on('connect', function () {
  console.log('connect') // no progress after this point. stops here
  getTopics(); // calling above function here to get topics
  subscribed.forEach(element => {
    client.subscribe(element);
    console.log(element);
  });

})

client.on('message', function (topic, message) {
  console.log(topic, '------>', message.toString());
})
你不应该混在一起。它们是处理异步流的不同技术,它们不能一起工作

使用一个或另一个,最好是后者,因为它更容易阅读和使用

async function getTopics(subName) {
  var qry = `SELECT topic_name from table where topic_name='${subName}'`

  // Convert `connection.query` to a `Promise` so we can properly
  // await it.
  const rows = await new Promise((resolve, reject) => {
    connection.query(qry, (err, rows) => {
      if (err) return reject(err)

      resolve(rows)
    })
  })

  return rows.map(row => row.topic_name)
})
从那时起,如果一个函数被标记为
async
,则需要
等待
在下一行中显示其结果:

client.on('connect', async function () {
  const topics = await getTopics()

  console.log(topics)
})

如果您计划使用
async/await
,您可能希望改用它,它会返回承诺。您只能在返回承诺的函数上使用
async/await

或者只使用内置承诺的mysql2。