正在尝试从 Table 存储查询中向 return 用户获取请求

Trying to return users from Table Storage Query to get request

我刚开始使用 Azure Table 存储,我正在尝试 return 来自查询的记录但是在我执行 queryEntities() 之后,result.entites 没有将记录保存到变量中。所以,在函数结束时,当我 return 那个变量是未定义的。

这是我的代码

db.service.ts

const azure = require('azure-storage');
const tableSvc = azure.createTableService('***', '***');

async function getAllRecords() {
  const query = new azure.TableQuery();

  let users = [];
  tableSvc.queryEntities('users', query, null, function(error, result, response) {
    if (!error){
      // here it logs the users succesfully
      console.log(result.entries);
      users = result.entries;
    }
  });

  // here it logs the variable with its starting value as empty array
  console.log(users);

  return users;
}

有人可以帮我吗?

您可以试试这个代码:

async function getAllRecords() {
  const query = new azure.TableQuery();

  let users = [];
  
  let func = new Promise(function(resolve,reject){
    tableSvc.queryEntities('users', query, null, function(error, result, response) {
      if (!error){
        // here it logs the users succesfully
        //console.log(result.entries);
        users = result.entries;
        resolve("success message");
      }
    })
  })
  
  func.then(() => {
    console.log(users);
  })
  return users;
}