I am getting error while fetching data in react-native - reactjs

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')

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>

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

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

Related

"data is not iterable" when reverse or sort the data using RTK Query

I am trying to use reverse & sort on the data coming from the RTK query. Whenever, i revers, it's showing me the error.
'data is not iterable'
Here's my code.
const {data, error, isLoading} = useGetTransactionsQuery()
const [modalState, setModalState] = useState<boolean>(false)
console.log(data.reverse())
Here is my whole code:- GitHub.
And also, I tried this. And it didn't work at all.
data.reverse()
[...data].reverse()
Array.from(data).reverse();
In the case of Array.from(data).reverse();. This error showing.
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) at Function.from ()
Hi #JoyShaheb,
When you try this to reverse the elements in the data. You're getting this error.
data is not iterable.
It's indicated you will need to first convert it to an array. Then, you're using the Array.from function. But, when you try to use that method Array.from(data).reverse();.
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) at Function.from ()
This error indicated the data object is undefined. This can happen if the query is still being loaded or if there was an error executing the query. You can check the value of the isLoading and error variables to determine the current state of the query. If isLoading is true, is still being loaded. If error is not null, it means that there was an error to execute and the data object will not be available.
if (!isLoading && error === null) {
const reversedData = Array.from(data).reverse();
}
This will only try to reverse the data object if the query has completed successfully and the data object is available.
Here are the things you need to focus about. The useGetTransactionsQuery hook returns an object with the following properties:-
data: The data returned by the query. This will be undefined if the query is still being loaded or if there was an error executing the query.
error:- An error object if there was an error executing the query, or null if the query completed successfully.
isLoading:- A boolean value that is true if the query is still being loaded and false if the query has completed.

I am getting an error while fetching data in react native with redux

Here is the API link:
http://muslimsalat.com/london/true/5.json
<Text>Query: {responseMsg.query}</Text>
<Text>For: {responseMsg.for}</Text>
<Text>Prayer Method: {responseMsg.prayer_method_name}</Text>
<Text>Daylight: {responseMsg.daylight}</Text>
The above lines are working.
I am able to fetch data from this API like, query, for, daylight, city, country and more, 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>Times: {responseMsg.items.fajr}</Text>
The above line is not working. Can anyone tell what I am doing wrong?
responseMsg.items is an array. Use map to get times:
<Text>Times: {(responseMsg.items || []).map(item => item.fajr).join(',')}</Text>

Cannot access Google Geocoding API response object

my first message :)
well, i am trying to write a weather Spa with angular.
as part of the model , i need to grab the location Lat and Lon so im using google geocoding api.
after using $resource.get i am successfully getting the object from google but cant access it.
here is my code :
weatherApp.controller('forecastController'['$scope','location','$resource',function($scope,location,$resource){
$scope.toGoogle = location.toGoogle;
$scope.resource = $resource('http://maps.googleapis.com/maps/api/geocode/json?');
$scope.georesult = $scope.resource.get({address:$scope.toGoogle,Key:"somekey"})
console.log(angular.fromJson($scope.georesult));
}]);
$scope.toGoogle is just a var holding the address which needs to be translated into x and y
the thing is - i am getting a response with the object i was expected, but when i am trying to access it with:
console.log(angular.fromJson($scope.georesult.results[0].geometry.location.lat));
I am getting "TypeError: Cannot read property '0' of undefined"
(Please note that i have already transformed the Json into regular object with Angular.fromJson);
Any suggestions ? thanks in advance.

go-endpoint Invalid date format and method not found

Hy, I have some problems with the Go endpoints and Dart client library.
I use the Go library https://github.com/crhym3/go-endpoints and the dart generator https://github.com/dart-lang/discovery_api_dart_client_generator
The easy examples works fine. But they show never how to use time.Time.
In my project, I have a struct with a field:
Created time.Time `json:"created"`
The output in the explorer looks like this:
"created": "2014-12-08T20:42:54.299127593Z",
When i use it in the dart client library, I get the error
FormatException: Invalid date format 2014-12-08T20:53:56.346129718Z
Should I really format every time fields in the go app (Format Timestamp in outgoing JSON in Golang?)?
My research come to that the dart accept something:
t.Format(time.RFC3339) >> 2014-12-08T20:53:56Z
Second problem, if comment out the Created field or leave it blank. I get a other error:
The null object does not have a method 'map'.
NoSuchMethodError: method not found: 'map' Receiver: null Arguments:
[Closure: (dynamic) => dynamic]
But I can't figure it out which object is null. I'm not sure if I'm using the Dart client correct
import 'package:http/browser_client.dart' as http;
...
var nameValue = querySelector('#name').value;
var json = {'name':nameValue};
LaylistApi api = new LaylistApi(new http.BrowserClient());
api.create(new NewLayListReq.fromJson(json)).then((LayList l) {
print(l);
}).catchError((e) {
querySelector('#err-message').innerHtml=e.toString();
});
Does anyone know of a larger project on github with Go endpoint and Dart?
Thanks for any advice
UPDATE[2014-12-11]:
I fixed the
NoSuchMethodError
with the correct discovery url https://constant-wonder-789.appspot.com/_ah/api/discovery/v1/apis/greeting/v1/rest
The problem with the time FormatExcetion still open, but I'm one step further. If i create a new item, it doesn' work. But if I load the items from the datastore and send it back, this works.
I guess this can be fixed with implementing Marshaler interface, thanks Alex. I will update my source soon.
See my example:
http://constant-wonder-789.appspot.com/
The full source code:
https://github.com/cloosli/greeting-example

Restangular data sent is being splitted

Restangular.one('suppliers', 'me').getList('websites').then(
(data) ->
$scope.websites = data
$scope.websites.patch()
)
I'm just trying this for a quick test.
So the api call on /suppliers/me/websites returns an array but when I try to patch from the Restangular object it sends the data splitted as you can see below.
[{"0":"h","1":"t","2":"t","3":"p","4":":","5":"/","6":"/","7":"w","8":"w","9":"w","10":".","11":"p","12":"f","13":"c","14":"o","15":"n","16":"c","17":"e","18":"p","19":"t","20":".","21":"c","22":"o","23":"m"}]
I'm new to Angular & Restangular , what am I missing ?
Edit : To be clear, I insta patch for the test but normally I modify the websites array by adding / removing.
It looks as if you're returning a string from your service, whereas a valid JSON response is expected by Restangular.
For example:
[{"website": "http://www.example.com"}, {"website": "http://www.domain.com"}]
EDIT: I just noticed that in your question, you say your service returns an array. Double-check what it does return and make sure that it is valid JSON.
EDIT 2: It seems that Restangular expects not only valid JSON, but also JSON formatted as my code sample above is (ie. [{"key": "value"}] and not ["value"].

Resources