【问题标题】:How to convert the auth response into array of objects?如何将身份验证响应转换为对象数组?
【发布时间】:2020-05-01 21:26:04
【问题描述】:

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

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】:

    你有一个竞争条件。当您执行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");
          }
        );
    

    【讨论】:

    • 我得到了输出,但结果是 1000 和一个 1000 的数组,但我想将所有值存储在一个数组中。就像我有 5000 个文档,我一次得到 1000 个文档,所以要获取 5000 个文档的结果,有 5 个数组,我想将数组存储在单个数组中
    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2020-03-31
    • 2016-02-25
    相关资源
    最近更新 更多