我有一个数组,我正在尝试读取和存储一个状态,但是使用下面的代码,这是我得到的错误:“ function collectionReferenace.doc()要求其第一个参数的类型为非空字符串,但未定义”,为什么会发生这种情况以及解决方案是什么?
componentDidMount(){
dbh.collection('Groups').doc(this.props.route.params.groupName)
.collection('Enrolled').doc('ids').get()
.then(querySnapshot => {
querySnapshot(doc => {
this.setState({
playerIDs: doc.data().players
})
})
})
console.log(this.state.playerIDs)
}
this.props.route.params.groupName
从上一个屏幕的导航中传递。当我在render函数中为其设置一个常量并调用该变量时,它可以正常工作。
render() {
const groupName = this.props.route.params.groupTitle
return
<Text>{groupName}</Text>
供您参考,这是我如何获得该route.param
<GroupComponent onPress = {
() => this.props.navigation.navigate
('Attendance', {groupTitle: group.GroupName, groupID: group.id})
} />
答案 0 :(得分:1)
根据您的代码,您应该使用dbh.collection('Groups').doc(this.props.route.params.groupTitle)
而不是groupName
。通过参数传递的对象具有键groupTitle
和键groupID
,而不是groupName
。