Calling the property of an object returns object instead of property

Joe Pringle

The title of this is admittedly confusing, but that's because i barely understand what's happening enough to describe it.

I have a react app which is suppose to list the first 5 pictures the mars rover has taken on sol 1000 using the NASA API.

The API returns an object in the format of Object{photos[0-999].img_src}. I slice object.photos to make a new object (or array, not sure) consisting of the first 5 items of the photos array within the object, but each of the items is itself an object containing various data about the image, when all i need is the image url.

To get the url, i use the .map method to create a new array and assign the url's of each item to it.

Finally, i use the .map method again to display each of images by inserting the url into the image source.

However, the inspector tools how that instead of each element source being the url string, they are all '[object Object]'.

I have no idea what is happening, why it's happening, or what to do to stop it. Can anyone shed light on this?

import "./App.css"
import { useState } from "react"

const App = () => {
  const [data, setData] = useState([])

  const handleFetch = async () => {
    const response = await fetch(`https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1000&api_key=${process.env.REACT_APP_NASA_KEY}`, {
      method: "GET",
    })
    const fetchData = await response.json()
    const fetchData2 = fetchData.photos.slice(0,5)
    setData(fetchData2.map(arrayItem => {
      return arrayItem.img_src
    }))

  }
  
  return (
    <div className="App">
      <h1>Mars rover images</h1>
      <button onClick={handleFetch}>Click me</button>
      <ol>
        {data && 
          data.map((temp, index) => {
            return (
              <li key={index}>
                <img src={{temp}} alt="" />
              </li>
            )
          })}
      </ol>
    </div>
  )
}


export default App
endmaster0809

You should use temp like below.

<img src={temp} alt="" />

not {{temp}}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ComboBoxItems displaying object instead of property when selected

From Dev

cannot access json object property returns undefined

From Dev

Angular httpClient returns property does not exist on object

From Dev

JS Object property returns 'NaN' on passing expression

From Dev

Displaying property object instead of value in Flask template

From Dev

Laravel Blade: calling property in object results in NULL

From Dev

Struct not calling property observers on new object

From Dev

changing object property values returns null in view

From Dev

Calling a function within an object property from another property

From Dev

Included property returns object that it's included to

From Dev

Event Object property "source" returns [object Object]

From Dev

Model property treated as object instead of string in formatOptions

From Dev

Calling the value of a variable as a property of an object in JavaScript

From Dev

Dynamically creating a getter that returns a property of the object

From Dev

Javascript - calling an external function as a property of an object

From Dev

JS Calling Object Property by Variable

From Dev

Mongoose find() returns undefined property and strange object

From Dev

NSubstitute - setting a property of the calling object

From Dev

Randomly calling an object property

From Dev

For in getting object property identifier instead of property value?

From Dev

Object returns own property and preserves methods

From Dev

Passed an object instead of object's property?

From Dev

Object's float property returns inconsistent values

From Dev

React property is an empty object instead of intended array

From Dev

Anonymous Problem while calling object property

From Dev

Mongodb FindOne returns object with undefined property

From Dev

object is set but property returns undefined

From Dev

Why do I get [object Object] instead of the object property value?

From Dev

calling function stored as a property in an object in a for loop

Related Related

  1. 1

    ComboBoxItems displaying object instead of property when selected

  2. 2

    cannot access json object property returns undefined

  3. 3

    Angular httpClient returns property does not exist on object

  4. 4

    JS Object property returns 'NaN' on passing expression

  5. 5

    Displaying property object instead of value in Flask template

  6. 6

    Laravel Blade: calling property in object results in NULL

  7. 7

    Struct not calling property observers on new object

  8. 8

    changing object property values returns null in view

  9. 9

    Calling a function within an object property from another property

  10. 10

    Included property returns object that it's included to

  11. 11

    Event Object property "source" returns [object Object]

  12. 12

    Model property treated as object instead of string in formatOptions

  13. 13

    Calling the value of a variable as a property of an object in JavaScript

  14. 14

    Dynamically creating a getter that returns a property of the object

  15. 15

    Javascript - calling an external function as a property of an object

  16. 16

    JS Calling Object Property by Variable

  17. 17

    Mongoose find() returns undefined property and strange object

  18. 18

    NSubstitute - setting a property of the calling object

  19. 19

    Randomly calling an object property

  20. 20

    For in getting object property identifier instead of property value?

  21. 21

    Object returns own property and preserves methods

  22. 22

    Passed an object instead of object's property?

  23. 23

    Object's float property returns inconsistent values

  24. 24

    React property is an empty object instead of intended array

  25. 25

    Anonymous Problem while calling object property

  26. 26

    Mongodb FindOne returns object with undefined property

  27. 27

    object is set but property returns undefined

  28. 28

    Why do I get [object Object] instead of the object property value?

  29. 29

    calling function stored as a property in an object in a for loop

HotTag

Archive