Reactjs Can';t获取Firestore子集合

Reactjs Can';t获取Firestore子集合,reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,我正在React+Firebase中开发应用程序。我的问题是子集合数据未定义 我的firestore收藏: users - XXXXXXXXXX - name: xxxxx ├ image: xxxxx └ shifts - monday - before_noon: true ├ after_noon: false

我正在React+Firebase中开发应用程序。我的问题是子集合数据未定义

我的firestore收藏:

users - XXXXXXXXXX - name: xxxxx
                   ├ image: xxxxx
                   └ shifts - monday - before_noon: true
                                     ├ after_noon: false
                                     └ night: true
错误:

无法读取未定义的属性“星期一”

我的代码:

{user.shifts.monday.before_noon}

如何调用Firestore子集合下的数据。

子集合与Firestore文档中的嵌套数据不同。对于嵌套数据,您可以从单个文档获取访问所有内容。例如:

const db = firebase.firestore();

await db.collection('users').doc('alice').set({
  name: 'Alice',
  shifts: {
    monday: {before_noon: true}
  }
});

// this is "true"
await db.collection('users').doc('alice').get()).data.shifts.monday.before_noon;

另一方面,子集合是一个完全独立的文档集合,在逻辑上与父文档相关。您必须分别从其父文档中获取子集合:

const userRef = db.collection('users').doc('alice');
const shiftsRef = userRef.collection('shifts');

await shiftsRef.doc('monday').set({
  before_noon: true
});

(await userRef.get()).data() // does not include .shifts
(await shiftsRef.get()).docs.map(snap => {
  day: snap.id, data: snap.data()
}); // [{day: 'monday', data: {before_noon: true}}]

通常,当数据量有限且您几乎总是希望将其与其余数据一起提取时,应使用嵌套数据子集合当存在大量嵌套数据(即太大而无法放入文档)或数据并非总是需要与父数据一起提取时,应使用。

是否
shift
用户文档的子集合的名称?另外,你能分享你用来获取
用户
文档的代码吗(没有
轮班
)?非常感谢Michael。这真的很有帮助,我可以理解firestore子集合的工作原理。