Home » Javascript » Code won't add info to a FireBase database (in ReactJS)

Code won't add info to a FireBase database (in ReactJS)

Posted by: admin June 30, 2018 Leave a comment

Questions:

This is my code:

    const cookies = new Cookies();
    var name = (cookies.get('name'));
    var key = (cookies.get('key'));
    console.log(key);
    const theme = createMuiTheme({
      palette: {
        primary: amber,
        secondary: blue
      },
      root: {
        flexGrow: 1,
      },
      bottomView: {

        width: '25%',
        height: 50,
        backgroundColor: '#FF9800',
        justifyContent: 'center',
        alignItems: 'center',
        position: 'absolute',
        bottom: 0
      },
    });
    class Base {
      constructor() {}
    }

   class Homework extends Component {
  constructor(props) {
    super(props);

    this.state = {
      topicBox: null,
      payloadBox: null
    };

    this.publish = this.publish.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }
 publish(e) {
    const { name, value } = e.target;
    this.setState({ [name]: value});  // this will set your state of current input, that you were handling
    //this.setState({value: event.target.value});
  }
  handleChange(e) {
    const { name, value } = e.target;
    this.setState({ [name]: value}) 
    //this.setState({value: event.target.value});
  }


      async loadTest() {
        try {
          //grab courses
          const response = await fetch('https://classroom.googleapis.com/v1/courses?access_token=' + key);
          const json = await response.json();
          //coursemax vars will eventually be user defined
          var coursemax = [2, 2, 2, 2, 2, 2, 2, 2];

          if (json.courses != null) {
            for (var course = 0; course < json.courses.length && course < 9; course++) {
              //grab course info
              var coursework = await fetch('https://classroom.googleapis.com/v1/courses/' + json.courses[course].id + '/courseWork?access_token=' + key);
              var coursejson = await coursework.json();
              console.log(coursejson);
              var assignment = "";
              for (var assignmentnum in coursejson.courseWork) {
                if (assignmentnum <= coursemax[course] - 1) {
                  //add in assignment
                  assignment += "<p>" + coursejson.courseWork[assignmentnum].title + "</p>";
                  //"Due: "+coursejson.courseWork[assignmentnum].dueDate.month+"/"+coursejson.courseWork[assignmentnum].dueDate.day
                }
              }
              //making ids to render
              document.getElementById('class' + (course + 1) + 'info').innerHTML = assignment;
              document.getElementById('class' + (course + 1)).innerHTML = json.courses[course].name + '</b>' + ':' + '<br/>';;
            }
          }
        } catch (err) {
          console.log(err);
        }
      }
    }
    var app = new Homework();
    var config = {
      apiKey: "xxx",
      authDomain: "xxx",
      databaseURL: "xxx",
      projectId: "xxx",
      storageBucket: "xxx",
      messagingSenderId: "xxx"
    };
    firebase.initializeApp(config);
    app.loadTest();

    function publish() {
      console.log(this.state.topicBox, this.state.payloadBox);
      const itemsRef = firebase.database().ref('Users');
      const item = {
        title: this.state.topicBox,
        user: this.state.payloadBox
      }
      itemsRef.push(item).set("name");
      this.setState({
        topicBox: '',
        payloadBox: ''
      });

    }

    const LoginPage = () =>
    <MuiThemeProvider theme={theme}>
          <div>
            <AppBar position="static" id='title'>
              <Toolbar>
                <Typography type="title" color='inherit'>
                  MVHS Homework App
                </Typography>
                <div id='avatar' color='inherit'><Avatar>{name[0]}</Avatar></div>
              </Toolbar>
            </AppBar>
       <form> 
        <input 
    type="text" 
    name="topicBox" 
    placeholder="Name" 
    value={ app.state.topicBox }
    onChange={ app.handleChange } 
  />
  <input 
    type="text" 
    name="payloadBox" 
    placeholder="Details"
    value={ app.state.payloadBox } 
    onChange={ app.handleChange } 
  />
  </form>
  <Button variant="raised" color="secondary" style = {theme.bottomView} onClick={ app.publish }>
    Add
  </Button>
          </div>

        </MuiThemeProvider>



    export default LoginPage

In the code above, there are to inputs and a button. The button should be pressed and add info in a database I have connected to. The info should include the inputted values under “names”

And the Error is:

Nothing Happens When I Hit The Button!

What’s Wrong?

EDIT: The error I believe is the only issue with the code right now. Feel free to tell me if any more errors. 🙂 (Am I nor establishing the car correctly?)

EDIT: I know that I have changed my question but please assist me.

Answers: