1

I am sending a json object using res.json. On the client side I am trying to set the json object to a piece of state.

Ive tried to .json() the response but that still does not let me assign it.

This is the server side sending the JSON File

app.get('/api/getPlace', async (req, res) => {
        const response = await client.search({
            searchType: "Coffee",
            location: "San Francisco, CA",
        })

        const foodPlace = response.jsonBody.businesses[9];

        console.log(foodPlace);
        res.json(foodPlace)
    })

Below is the whole component file to render the json object

import React, { Component } from 'react';
import axios from 'axios';

class RandomPlace extends Component {
    constructor(props) {
        super(props);
        this.state = {
            response: {},
        };
    }

    async componentDidMount() {
        const res = axios.get('/api/getPlace');
        this.setState({ response: res.data })

    }

    render() {
        return (
            <div>
                {this.state.response}
            </div>
        );
    }
}

export default RandomPlace;
0

2 Answers 2

2

The client call must be awaited:

async componentDidMount() {
    const res = await axios.get('/api/getPlace');
    this.setState({ response: res.data })

}
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error when I add await × Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.
That's because the response contains an object, and the <div> content must be HTML elements. Try to replace the <div> inner as follows: JSON.stringify(this.state.response)
0
import React, { Component } from 'react';
import axios from 'axios';

class RandomPlace extends Component {
    constructor(props) {
        super(props);
        this.state = {
            response: {},
        };
    }

    async componentDidMount() {
        const res = await axios.get('/api/getPlace');
        this.setState({ response: res.data })

    }

    render() {
        return (
            <div>
                {this.state.response}
            </div>
        );
    }
}

export default RandomPlace;

REST api calls are asynchronous, which means the code proceeds to the next statement without waiting for the api call to compelete. When await is adding before the call, the execution will pause till the call completes or timesout (if specified) before proceeding to the next line. async/await is a better alternative to promises.

2 Comments

I get this error when I add use your code × Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.
That's probably because your res object looks like this : { data: { field1:value1}}. If thats the case you should specify the exact field you want to use in the render() method as objects cannot be used as a react child element. You can do: {this.state.response.field1}

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.