Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Uncaught TypeError: this.state.messages.map is not a function

I'm developing a React application and I've run into a problem with this mapping function:

render() {
    const messages = this.state.messages.map((message) => {
        return (
            <li key={message[".key"]}>{message.text}</li>
        )
    })
    return (...)
}

I get the following error:

Uncaught TypeError: this.state.messages.map is not a function

I think it has something to do with the key. My messages state is filled using the componentDidMount function with Firebase:

componentDidMount() {
    firebase.database().ref("messages/").on("value", (snapshot) => {
        const messages = snapshot.val()
        if(messages != null) {
            this.setState({
                messages: messages
            })
        }
    })
}

And messages are added with this function:

submitMessage(e) {
    const nextMessage = firebase.database().ref("messages/").push({
        text: this.state.message
    })
}

This means my Firebase database with one message looks like this:

{
    "messages" : {
        "-KV6trWbEW73p8oS49Qk" : {
            "text" : "test"
        }
    }
}

My suspicion is that I am identifying the key incorrectly. Any help with solving this issue would be much appreciated!

like image 361
Barry Michael Doyle Avatar asked Oct 27 '16 21:10

Barry Michael Doyle


People also ask

How do I fix .map is not a function?

The "TypeError: map is not a function" occurs when we call the map() method on an object that is not an array. To solve the error, console. log the value you're calling the map() method on and make sure to only call the map method on valid arrays.

How do you use the map function in react?

In React, the map method is used to traverse and display a list of similar objects of a component. A map is not a feature of React. Instead, it is the standard JavaScript function that could be called on an array. The map() method creates a new array by calling a provided function on every element in the calling array.

What is TypeError in react?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function. Here is an example of how the error occurs.

How do you map an object in react?

To map through an object's value in React:Use the Object. values() method to get an array of the object's values. Call the map() method on the array of values.


1 Answers

The problem is that the response from the database is an object with keys, whereas map() needs to be called on an array. The easiest way to loop through an object is to use Object.keys() and then use bracket notation to access the values.

render() {
    const messages = Object.keys(this.state.messages).map((key) => {
        return (
            <li key={key}>{this.state.messages[key].text}</li>
        )
    })
    return (...)
}
like image 102
Andy Noelker Avatar answered Sep 19 '22 21:09

Andy Noelker