1

I am trying to map data that I have in an array, however, the data is not mapped when I run the code. If I log the data (serials variable), it logs the variables that I would like to map (screenshot below). Does anyone have any suggestions about why the data isn't mapped?

numberList() {
  const uid = this.state.user.uid;
    const serialRef = db.ref(uid + "/serials");
    
    serialRef.on("value", (serial_numbers)=> {
      const serials = [];
      serial_numbers.forEach((serial_number)=> {
        serials.push({s:serial_number.val()});
      });
      console.log(serials);
  return (
    <ul>
      {serials.map((number) => <li>{number}</li>)};
    </ul>
  );
    });

}
        render (){
        return (
          <button onClick={this.numberList}>Cards</button>
        )};

}

Screenshot of the data

Thanks in advance!

2
  • > the data is not mapped when I run the code how did you notice this? Commented Nov 10, 2020 at 6:44
  • When I click on the button the list doesn't appear on the page. Commented Nov 10, 2020 at 8:55

2 Answers 2

1

From your log I assume the map should be as below to get exact number.

 return (
    <ul>
      {serials.map((number) => <li>{number.s.serial}</li>)};
    </ul>
  );
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. However, it still doesn't show the values when I click the button, only the array gets logged in the console.
1

The "numbers" you are mapping are objects (according to the log you attached), but React JS doesn't allow to display objects directly - you probably only want to display the value anyway. You should also include a key when generating a view from an array of objects. The correct code should be this:

<ul>
      {serials.map((number, i) => <li key={i}>{number.s.serial}</li>)};
</ul>

2 Comments

Thanks for your reply. But the list of data still doesn't appear on the page when I clik the button.
Do you get any errors? Is it a blank screen or are other elements rendered?

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.