将数组作为数据集发布到firebase实时数据库

时间:2018-06-16 13:48:36

标签: reactjs firebase

我发现你无法数组推送到 firebase 数据库...我有一个字符串数组状态的形式传递给组件,我想将数组推送到我的DataBase。我可以在锻炼表中发布对象...并且创建了workoutDate属性/键,但是没有创建exerciseList属性。我必须在firebase中将此数组作为数据集发布,有什么建议吗?

注意:当我在console.log(workoutArray)时,它会记录正确的状态,这意味着此时状态正确。

this.state.workoutArray example = ['俯卧撑','卧推','dumbell flys']

以下代码:

import React, { Component } from 'react';
import '../styles/navbar.css'
import fire from '../config/fire'
import NavBar from './NavBar';
import WorkoutForm from '../dashboard/WorkoutForm'
import Dashboard from '../dashboard/Dashboard';




class Home extends Component {
constructor(props) {
    super(props)

    this.state = {
        show: false,
        selected: [],
        workoutArray: []
    }
}


showForm = function () {
    this.setState({ show: true })
}.bind(this)

setSelectedState = function (data) {
this.setState({selected: data})
}.bind(this)

onCreate= function(event) {
    event.preventDefault()
    let workoutArray = this.state.selected
    this.setState({workoutArray: workoutArray})
    this.setState({show: false})
    console.log(workoutArray)
    const DBRef = fire.database().ref('workout');
    const workout = {
        workoutList: {},
        workoutDate: Date.now()
    }
    setTimeout(function(){DBRef.push(workout)}, 5000)
    console.log(workoutArray)

}.bind(this)



render() {


    return(

        <div>

        <NavBar showForm={this.showForm} />
        {this.state.show ?
        <WorkoutForm
        setSelectedState={this.setSelectedState}
        selected={this.state.selected}
        onCreate={this.onCreate}
        showForm={this.showForm}
        show={this.state.show} />
         : <Dashboard workoutArray={this.state.workoutArray} />  }
       </div>


    )
}
}

export default Home

0 个答案:

没有答案