如何将身份验证响应转换为对象数组?

How to convert the auth response into array of objects?

我正在尝试使用 auth 函数获取用户的响应,我必须使用 xlsx-populate 库创建一个 excel sheet,并且我能够将其转换为数组对象的数量,因为限制是 1000,所以有多个对象数组。而且我无法弄清楚我该如何解决 problem.in 这个问题,我只是使用 auth 获取结果并尝试将结果放入对象数组中。我也尝试使用对象传递到 excel sheet 但它给出了 excel sheet 最后 1000 个查询响应

const admin = require("firebase-admin");
const momentTz = require("moment-timezone");
const XlsxPopulate = require("xlsx-populate");
momentTz.suppressDeprecationWarnings = true;
const {
  alphabetsArray
} = require("./constant");
var start = momentTz().subtract(4, "days").startOf("day").format();
var start = momentTz(start).valueOf();
const end = momentTz().subtract(1, "days").endOf("day").format();
const  listAllUsers = async(nextPageToken) =>{
  const [workbook] = await Promise.all([
    XlsxPopulate.fromBlankAsync()
  ]);
  const reportSheet = workbook.addSheet("Signup Report");
  workbook.deleteSheet("Sheet1");

  reportSheet.row(1).style("bold", true);
  [
    "Date",
    "TIME",
    "Phone Number"
  ].forEach((field, index) => {
    reportSheet.cell(`${alphabetsArray[index]}1`).value(field);
  });
  let count = 0
  // List batch of users, 1000 at a time.


  const data = [];
  admin
    .auth()
    .listUsers(1000, nextPageToken)
    .then (async  (listUsersResult) => {

      listUsersResult.users.forEach((userRecord) =>{

        const time = userRecord.metadata.creationTime;

        const timestamp = momentTz(time).valueOf();
        //   console.log(timestamp)


        if (timestamp >= 1585704530967 ) {
          console.log(time);
          let column = count+2;
          count++;
          data.push(userRecord.toJSON())
          reportSheet.cell(`A${column}`).value(time);

          reportSheet.cell(`C${column}`).value(userRecord.phoneNumber);

        }
      });

   console.log(JSON.stringify(data))//this is the array of the object and i am getting after 1000 response 
      if (listUsersResult.pageToken) {

        // List next batch of users.
        listAllUsers(listUsersResult.pageToken);
        await workbook.toFileAsync("./SignUp.xlsx");
      }
    })
    // .catch(function (error) {
    //   console.log("Error listing users:", error);
    // });
    // const datas = []
    //   datas.push(data)
    //   console.log(datas)
    return ;
}
// Start listing users from the beginning, 1000 at a time.
listAllUsers();


and the output i am getting is like this 
[]
[]
[]
[]
[]
i want to convert this into a single array of response

您有竞争条件。当您执行 console.log(JSON.stringify(data)) 时,您的 listUserQuery 正在进行中(并且处于异步模式)并且您在打印数组时还没有答案。因此数组为空。

试试这个(我不确定这个最佳解决方案,我不是 nodeJS 开发人员)

  admin
    .auth()
    .listUsers(1000, nextPageToken)
    .then (async  (listUsersResult) => {

      listUsersResult.users.forEach((userRecord) =>{

        const time = userRecord.metadata.creationTime;

        const timestamp = momentTz(time).valueOf();
        //   console.log(timestamp)


        if (timestamp >= 1585704530967 ) {
          console.log(time);
          let column = count+2;
          count++;
          data.push(userRecord.toJSON())
          reportSheet.cell(`A${column}`).value(time);

          reportSheet.cell(`C${column}`).value(userRecord.phoneNumber);

        }
      }
      console.log(JSON.stringify(data))//this is the array of the object and i am getting after 1000 response 
      if (listUsersResult.pageToken) {

        // List next batch of users.
        listAllUsers(listUsersResult.pageToken);
        await workbook.toFileAsync("./SignUp.xlsx");
      }
    );