如何从Firebase获取以下数据结构?

时间:2020-01-15 00:19:26

标签: javascript angular firebase google-cloud-firestore

这是我想从firebase获得的结构,它要求我提供格式化答案的接口,这是正确的还是正确的?

{
  "users":{
    "user1":{
        "username":"john",
        "full_name":"John Vincent",
        "created_at":"9th Feb 2015",
        "groups":{
            "group1":true,
            "group3":true
        }
        "last_logins":...
    },
    "user2": ...,
    "user3": ...
  }
  "groups": {
     "group1"{
        "group_name":"Administrators",
        "group_description":"Users who can do anything!",
        "no_of_users":2,
        "members":{
            "user1":true,
            "user3":true
        }
      },
     "group2"{
        "group_name":"Moderators",
        "group_description":"Users who can only moderate!",
        "no_of_users":1,
        "members":{
            "user2":true
        }
      }
   }
 }

这是我的代码... !!!

export interface Users {
    username: string;
    full_name: string;
    created_at: string;
    groups: any[];
}

export interface Group {
    group_name: string;
    group_description: string;
    no_of_users: Number;
    members: any[];
}
this.users = this.db.collection('users').snapshotChanges().pipe(
    map(actions =>
        actions.map(a => { const data = a.payload.doc.data() as Users; const id = a.payload.doc.id;
        return {id, ...data};
    }))
);

this.groups = this.db.collection('groups').snapshotChanges().pipe(
    map(actions =>
        actions.map(a => { const data = a.payload.doc.data() as Group; const id = a.payload.doc.id;
        return {id, ...data};
    }))
);

如何使界面查询用于从 firebase ??

获取此结构

1 个答案:

答案 0 :(得分:1)

您可以使用泛型类型来创建如下内容:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>truezip-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <id>remove-a-file-in-sub-archive</id>
            <goals>
                <goal>remove</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <fileset>
                    <directory>target/Samplewebapp.war/WEB-INF/lib/someJar.jar</directory>
                    <includes>
                        <include>broker.xml</include>
                    </includes>
                </fileset>
            </configuration>
        </execution>
    </executions>
</plugin>

并像这样使用它

export class FirebaseService<T> {

    constructor (collection: string) {
        this.dbInstance = fireDb //import
        this.collection = collection
    }
.
.
.
    async list(): Promise<T[]> { 
        return this.dbInstance 
            .collection(this.collection)
            .get()
            .then(async querySnapshot => { 
                const result: Array<T> = [];
                querySnapshot
                    .forEach(doc =>
                         result.push(doc.data())
                     ) 
                return result; 
            })
    }
}

这样,您可以指定对象的类型,并且仍然具有通用且可预测的方式来处理数据