首页 文章

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

提问于
浏览
0

我正在使用Cloud Firestore作为我的Ionic / Cordova应用程序的数据库 .

我的问题是试图从Firestore数据库“获取”存储的数据并在我的“计算功能”中使用它们 .

我在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)

我当前的代码能够从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);
    });

此doc.id显示文档名称,doc.data()显示该文档中的Fields值 .

此函数检索集合中的所有文档 . (按照预期)我尝试使用Observable with Interface但无法使其工作 .

Is there a way to dynamically save retrieve data to an object of some kind and then use it in other functions?

谢谢

1 回答

  • 3

    我相信你需要映射数据,对吗?

    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);
    });
    

相关问题