1

Here is the API link:

http://api.aladhan.com/v1/timingsByCity?city=Dubai&country=United%20Arab%20Emirates&method=8

<Text>{responseMsg.code}</Text>
<Text>{responseMsg.status}</Text>

The above lines are working. I am able to fetch data from this API like, code, status but I am unable to fetch prayer times from this API. I don't know how to write that line to fetch prayer times from this API.

<Text>{(responseMsg.data || []).map(time => time.timings.Fajr)}</Text>
<Text>{(responseMsg.data || []).map(time => time.timings.Dhuhr)}</Text>

I am getting the following error:

TypeError: undefined is not an object
(evaluating '(responseMsg.data || {}).timings.Fajr')

2
  • What exactly is the error? Commented Feb 19, 2019 at 11:30
  • error is: TypeError: undefined is not an object (evaluating '(responseMsg.data || {}).timings.Fajr') Commented Feb 19, 2019 at 12:08

3 Answers 3

2

You should directly access the timings because data field is object and not an array. What you have to write is :

<Text>{(responseMsg.data || {}).timings.Fajr}</Text>
Sign up to request clarification or add additional context in comments.

9 Comments

no brother, It's also not working. error is: TypeError: undefined is not an object (evaluating '(responseMsg.data || {}).timings.Fajr')
Ohh yes! Because timings key is undefined when data is fetching. May be you can wrap it in ternary like this {responseMsg.data && responseMsg.data.timings ? <Text>{responseMsg.data.timings.Fajr}</Text> : null}
Or may be you can save it in variable like : const {data = {}} = responseMsg; const {timings = {}} = data;
It is under data > meta > method > name.. {responseMsg.data && responseMsg.data.meta ? <Text>{responseMsg.data.meta.method.name}</Text> : null}..... If i write like this then it's not working
It worked now brother. thanks for helping. last thing if you can help. i am using geolocation. how can i set geolocation in api link. because api accept city name and geolocation fetches only latitude and longitude.
|
1

.Map on responseMsg.data won't work as data is an object. Map is a function on arrays not object.

2 Comments

so how can i write for fetch without .map ?
No need to use map just access the object directly with responseMsg.data.timings.Fajr.
0

I know that my answer is sooo late ,since i was searching for the same issue

the trick is that in the output json nested objects,ther is another child data object so to access prayers times :

fajr=response.data.data.timings.fajr

yes data.data

timezone=response.data.data.meta.timezone
hijriDay=response.data.data.date.hijri.day
hijriMonth=response.data.data.date.hijri.month.en
hijriYear=response.data.data.date.hijri.year

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.