简体   繁体   English

如何从Cloud Firestore获取数据并将其转换为本地可用数据(Cordova / Angular)

[英]How to get data from Cloud Firestore and Translate it into Local Usable Data (Cordova/Angular)

I am using Cloud Firestore as a Database for my Ionic/Cordova App. 我将Cloud Firestore用作Ionic / Cordova应用程序的数据库。

My problem is trying to "get" the stored data from Firestore database and use them in my "calculation functions". 我的问题是尝试从Firestore数据库中“获取”存储的数据并在我的“计算函数”中使用它们。

My Database structure in Firestore is as follows: 我在Firestore中的数据库结构如下:

UniqueID (collection)
                       >
                         1000  (document)
                                           >
                                              Year (field)
                                              Info (field)
                         1020  (document)
                                           >
                                              Year (field)
                                              Info (field)
                         8394  (document)                                               
                                              Year (field)
                                              Info (field)
                         4543  (document)
                                           >
                                              Year (field)
                                              Info (field)

My current code is able to retrieve/get data from Firestore however I can't utilize this data yet as i couldn't find a way to do that. 我当前的代码能够从Firestore检索/获取数据,但是由于无法找到一种方法,因此我还无法利用这些数据。

Get data function: 获取数据功能:

firebase.firestore().collection(`${user.uid}`).get().then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
        });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    });

This doc.id is showing the document name and doc.data() is showing the Fields value inside that document. 此doc.id显示文档名称,而doc.data()显示该文档内部的Fields值。

This function retrieve all documents inside the collection. 此函数检索集合中的所有文档。 (as intended) I tried using Observable with Interface but couldn't get it to work. (按预期)我尝试将Observable与Interface结合使用,但无法使其正常工作。

Is there a way to dynamically save retrieve data to an object of some kind and then use it in other functions? 有没有一种方法可以动态地将检索数据保存到某种对象,然后在其他功能中使用它?

Thank you 谢谢

I believe you need to map the data, right? 我相信您需要映射数据,对吗?

function list(user) {
  return firebase.firestore()
    .collection(user.uid)
    .get()
    .then(snapshot => {
      const list = [];

      snapshot.forEach(doc => {
        const data = doc.data()
        data.id = doc.id;
        list.push(data);
      });

      return list;
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });
}

list({ uid: 'some-valid-uid' }).then((list) => {
  console.log(list);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM