javascript - 使用 firebase 数据填充下拉字段

标签 javascript reactjs firebase firebase-realtime-database

我在控制台中获取值,但无法在下拉列表中显示 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} />

最佳答案

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

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

关于javascript - 使用 firebase 数据填充下拉字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60094812/

相关文章:

javascript - Jquery onClick 函数在 React 中未定义

javascript - “属性值未定义” - 如何修复未定义的属性?

android - Firebase 有不同的编译和运行版本

javascript - 检测查询参数 react-router-dom v4.x 的变化并重新渲染组件

reactjs - 'this.setState' 有什么问题

ios - Firebase 类型 'AppDelegate' 不符合协议(protocol) 'FUIAuthDelegate'

android - 如何在 Firebase 中更改值并重新触发 eventListener?

javascript - jQuery/JavaScript 和 document.ready() 文档中

javascript - :eq() and :nth-child()之间的jQuery区别

javascript - webpack -p 无法丑化 es2015 代码