1

i am having trouble pulling an object that i have on state and showing it on my web page. it doesn't exactly matter how it appears on the web page i just want to figure out how to view it so far.

import React, { Component } from 'react';
import {Link} from 'react-router-dom';
import axios from 'axios'
const test = require('../controller')

export default class Calandar extends Component {
  constructor(props) {
    super(props)

    this.state = {
      avalable: {},
      unavalable: {}
    }

  }


  componentDidMount() {
    axios.get('/api/avalable').then(res => {
      // console.log(res.data)
      this.setState({avalable: res.data})
    })
  }




  render() {
    return (
      <div>
        Calandar.js
      <div>
        {console.log(this.state.avalable)}
      </div>
      </div>
    );
  }
}

this is the array that i have saved by doing a get request from my database.

0: {…}
avalable: true
date: "1-1-1"
dentist: "dentist_test"
id: 1
name: "dentist_test"
office: "test_office"
time: "01:01:01"

2
  • 1
    What is the error you are getting in the console? Commented Jan 16, 2018 at 19:26
  • 1
    console.log() inside JSX will not work because it returns undefined, you can try {JSON.stringify(this.state.avalable)}, then to render each entry you can use map function. Commented Jan 16, 2018 at 20:16

2 Answers 2

1

Unless I'm missing something, drop the console.log in your JSX, should just be:

{this.state.avalable}

But more specifically, if you want the dentist name to appear to show from the object, use:

{this.state.avalable.dentist}
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this but it doesn't show anything or even send an errow
and if you uncomment "// console.log(res.data)", you are indeed getting an Object? I'm not familiar with what the axios Promise is returning.
0

i found the answer, i just put it in an array instead of an object here is the full code, thanks

import React, { Component } from 'react';
// import {Link} from 'react-router-dom';
import axios from 'axios'

export default class Calandar extends Component {
  constructor(props) {
    super(props)

    this.state = {
      avalable: [],
      unavalable: []
    }

  }


  componentDidMount() {
    axios.get('/api/avalable').then(res => {
      // console.log(res.data)
      this.setState({avalable: res.data})
    })
  }




  render() {
    console.log(this.state.avalable)
    var availabeList = this.state.avalable.map(available => (<div key={available.id}>
      {available.dentist}
    </div>))
    return (
      <div>
        Calandar.js
      <div>
        
        {availabeList}
      </div>
      </div>
    );
  }
}

1 Comment

i know this wasn't exactly what i was asking but it worked for my scenario

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.