Rupert Turnbull
Rupert Turnbull

Reputation: 11

React Native to fetch from a REST API won't work because of hyphen

My fetch returns this promise which works for other fields in the API but I need to save the value for a field within that has the name "datasets-pollencheck_apiaries" however react-native inteprets the "-" as something else and I can't access this field, and continually get the error that says "Can't find variable: pollencheck_apiaries"

.then((response) => response.json())
.then((responseJson) => {
    LINK = responseJson.links.datasets-pollencheck_apiaries;

})

Any insight would be much appreciated.

Upvotes: 1

Views: 90

Answers (2)

John Lodan
John Lodan

Reputation: 41

This is because JavaScript thinks you're trying to do a math operation. You can also access object properties with bracket notation.

You should try this:

LINK = responseJson.links['datasets-pollencheck_apiaries']

Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

Upvotes: 3

Alex
Alex

Reputation: 69

try to change to

.then((response) => response.json())
.then((responseJson) => {
    LINK = responseJson.links["datasets-pollencheck_apiaries"];

})

Upvotes: 0

Related Questions