如何将auth响应转换为对象数组?

问题描述 投票:0回答:1

我试图使用Auth函数获取用户的响应,我必须使用xlsx-populate库创建一个excel表,我能够将其转换为对象数组,因为限制是1000,所以有多个对象数组,我无法弄清楚如何才能解决这个问题。在这个问题中,我只是简单地使用Auth获取结果,并尝试将结果转换成对象数组。

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
node.js google-cloud-firestore google-cloud-functions google-authentication xlsx-populate
1个回答
0
投票

你有一个竞赛条件。当你执行你的 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");
      }
    );
© www.soinside.com 2019 - 2024. All rights reserved.