firebase 如何在firestore中获取子集合数据?

dvtswwa3  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(113)

我已经建立了一个Web应用程序使用节点表达到后端和前端我用Reactjs。在firestore数据库有一个“用户”集合在那里有很多文档为每个用户。为每个文档有字段和子集合。

第一个视图

第二个视图(在子集合视图中)

这是一个与我的真实的数据库结构类似的示例数据库。我想获取所有用户(在用户表文档中),以及子集合字段。
对于每个用户都有相同的子集合。(就像这个图像用户有4个子集合一样,另一个用户也有相同的子集合。)

为此,我编写了如下代码。

模型类

class Users {
    constructor(id,name,email,provider,firstWord,leda,age,birthday,district,gender,familyChildren,fatherEducation,monthlyIncome,motherEducation,whichChild,awaSE,awaUN,kathakaraaSE,kathakaraaSE,kathakaraaUN) {
            this.id = id;
            this.name = name;
            this.email = email;
            this.provider = provider; 

            this.email = firstWord;
            this.email = leda;
           
            this.age = age;
            this.birthday = birthday; 
            this.district = district; 
            this.gender = gender; 

            this.familyChildren = familyChildren;
            this.fatherEducation = fatherEducation;
            this.monthlyIncome = monthlyIncome;
            this.motherEducation = motherEducation; 
            this.whichChild = whichChild;
            
            this.awaSE = awaSE; 
            this.awaUN = awaUN; 
            this.kathakaraaSE = kathakaraaSE; 
            this.kathakaraaUN = kathakaraaUN; 
           
      
    }
}
module.exports = Users;

控制器

'use strict';

const firebase = require('../db');
const Users = require('../models/users');
const firestore = firebase.firestore();

const getAllUsers = async (req, res, next) => {
    try {
        const users = await firestore.collection('users');
        const data = await users.get();
        const userArray = [];
        if(data.empty) {
            res.status(404).send('No user  found');
        }else {
            data.forEach(doc => {
                const users = new Users(
                    doc.id,
                    doc.data().name,
                    doc.data().email,
                    doc.data().provider,
                    doc.data().firstWord,
                    doc.data().leda,
                    doc.data().age,
                    doc.data().birthday,
                    doc.data().district,
                    doc.data().gender,
                    doc.data().familyChildren,
                    doc.data().fatherEducation,
                    doc.data().monthlyIncome,
                    doc.data().motherEducation,
                    doc.data().whichChild,
                    doc.data().awaSE,
                    doc.data().awaUN,
                    doc.data().kathakaraaSE,
                    doc.data().kathakaraaUN,
                    
                );
                userArray.push(users);
            });
            res.send(userArray);
        }
    } catch (error) {
        res.status(400).send(error.message);
    }
}

module.exports = {

    getAllUsers,

}

路由器类

const router = require("express").Router();
const { getAllUsers } = require('../controllers/userscontroller.js')
 

router.get('/AllUsers', getAllUsers);

 
module.exports = router;

模型类图像

1.用户集合字段2. childGrow集合字段3. childPrivateDetails集合字段4. famyDetails集合字段5. wenath集合字段
但输出是


中没有显示其他集合字段。

我如何使用节点表达式来实现这一点?

h7appiyu

h7appiyu1#

您可以执行以下操作:

const getAllUsers = async (req, res, next) => {
  try {
    const users = await getFirestore().collection('users');
    const data = await users.get();
    const userArray = [];
    if (data.empty) {
      res.status(404).send('No user  found');
    } else {
      for (let doc of data.docs) {
        let firstSubCollectionData = await getFirestore().collection('users').doc(doc.id).collection('firstSubCollection').get();
        let secondSubCollectionData = await getFirestore().collection('users').doc(doc.id).collection('secondSubCollectionData').get();
        let thirdSubCollectionData = await getFirestore().collection('users').doc(doc.id).collection('thirdSubCollectionData').get();
        let forthSubCollectionData = await getFirestore().collection('users').doc(doc.id).collection('forthSubCollectionData').get();
        // construct your object here
        // add it to list
      }
      res.send(userArray);
    }
  } catch (error) {
    res.status(400).send(error.message);
  }
}

"但是...“
子集合可能是一个潜在的值列表。如果不是,我认为你必须重新设计你的数据模型。

相关问题