0

I have this Api call, which returns my contact list, and is supposed to save them as an array of objects into this.state.contacts, so that I can of course reference them in other places.

The line that says console.log(result) logs exactly what I expect... which is an array of objects containing all of my contact list data.

But after that, the console.log(Contacts: ${this.state.contacts} logs instead of the same data in result:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[obj
class Database extends Component {
        constructor(props){
            super(props);

            this.state = {
                clients: [],
                contacts: []
            }
        }

        componentDidMount() {
            fetch(API_URL + `/contacts/all`)
                .then(res => {
                     if (!res.ok) {
                         throw new Error();
                       }
                       return res.json();
                      })
                        .then((result) => {
                            this.setState({ contacts: result });
                            console.log(result);
                            console.log(`Contacts: ${this.state.contacts}`);
                        })
                        .catch(error => {
                            console.log(error);
                        })
                );
        }

What am I missing here? I feel like I've done it like this before and it worked fine.

3 Answers 3

2

This is because there is type coercion in your expression. Try to output this: console.log('Contacts: ${JSON.stringify(this.state.contacts)}'); so your object wont be called by ToString but rather JSON.stringify will work first.

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

Comments

1

That's because you are asking JS to turn your object (this.state.contacts) into a string. Some options:

console.log(`Contacts: ${JSON.stringify(this.state.contacts)}`);
console.log(`Contacts: ${JSON.stringify(this.state.contacts, true, 2)}`); // pretty-reprinted JSON
console.log('Contacts:', this.state.contacts);

Comments

0

Use JSON.stringify(object) to change it to a string before you use console.log.

Comments

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.