【问题标题】:Populate dropdown fields with firebase data [duplicate]使用 Firebase 数据填充下拉字段 [重复]
【发布时间】:2020-02-06 12:17:18
【问题描述】:

我在控制台中获取值但无法在下拉列表中显示 See this link

谁能解释我在这里做错了什么

        return firebase.database().ref('Users/Trainers/').on('value', (snapshot) => {
            snapshot.forEach(function(childSnapshot){
                // var childKey= childSnapshot.key;
                var childData= childSnapshot.val();
                var childEmail = childData.email;
                var childfirstName = childData.firstName;
                var childlastName = childData.lastName;
                var childTrainers = childfirstName + ' ' + childlastName + ' ' + childEmail;
            console.log(childTrainers);
            })
        })
    }

    async componentDidMount(){
        this.getlist();
    }

状态

childTrainers: []

而在渲染方法中,

<Select options={this.state.childTrainers} />

【问题讨论】:

  • 你在哪里打电话给setState 更新的 childTrainers ?
  • 这就是我犯的错误。this.setState({ CourseName : [{label: {CName}}] }) 如果这样做,我会出错
  • TypeError: 无法读取未定义的属性“setState”
  • 请参阅stackoverflow.com/questions/59871401/…,了解如何从 Firebase 加载数据、将其设置为状态并从那里呈现数据的最小示例。

标签: javascript reactjs firebase firebase-realtime-database


【解决方案1】:

尝试从 firebase 获取值时更新状态:

return firebase.database().ref('Users/Trainers/').on('value', (snapshot) => {
    const childTrainerSnapshot = snapshot.map(childSnapshot => {
        return childfirstName + ' ' + childlastName + ' ' + childEmail
    });
    this.setState({ childTrainers: childTrainerSnapshot });
})

【讨论】:

  • 不,这对我不起作用
猜你喜欢
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多