以条目数组的形式检索整个文档

时间:2019-03-24 16:11:05

标签: javascript firebase google-cloud-firestore

我在Firestore中有一个具有以下结构的集合:

Publications   
     PubData <---- This is a Doc
        1892872 <---- This is a map  
          Name: abc
          Id: 123 
        1892875 <---- This is a map  
          Name: abc
          Id: 123 

查询PubData文档:

fb.publicationsCollection
  .doc("pubdata")
  .get()
  .then(res => {
   let pubData = res.data();
   })

返回以下数据:

{1892872: {…}, 1892875: {…}}

如何将其作为数组返回,以便可以使用for循环对其进行迭代?

2 个答案:

答案 0 :(得分:2)

您可以iterate an object's properties with a foreach loop

class Panel1 extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    public static final int DELAY = Frame1.DELAY;
    private Timer timer;
    private BufferedImage fighter;
    private BufferedImage background;
    int x, y;

    public Panel1() {
        timer = new Timer(DELAY, this);
        try {
            URL url = new URL(Frame1.SPRITE_IMAGE);
            // fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
            fighter = ImageIO.read(url);

            url = new URL(Frame1.ANDROMEDA_IMAGE);
            background = ImageIO.read(url);

        } catch (IOException e) {
            e.printStackTrace();
        }
        initComponents();
        timer.start();
    }

    public void initComponents() {
        this.setSize(Frame1.WIDTH, Frame1.HEIGHT);
        this.setDoubleBuffered(true);
        this.setBackground(new Color(0, 0, 0, 0));
        x = 150;
        y = 200;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, this);
        g.drawImage(fighter, x, y, this);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        this.repaint();
    }

    public void moveLeft() {
        x -= 10;
    }

    public void moveRight() {
        x += 10;
    }
}

由于您并没有真正说出数组要包含的内容,因此请您自行决定如何在最终数组中表示文档映射的键和值。

答案 1 :(得分:2)

由于您要求输入一组条目,因此可以使用Object.entries

await