1

We are encountering a rather strange phenomena with .map in our React-Native (version 0.59.3) project: The APK runs well on various physical devices or emulators, but not on Android Version 9.0 where it crashes with

TypeError: o.map is not a function. 
(In 'o.map(function(t,n){n.icon_color=A.white,
-1!==s.findIndex(function(n){return t.title==n.title})
&&(t.icon_color=A.red)})','o.map' is undefined)

This is how the error popup looks like:

Popup

We understand that this message appears if the variable on which we are applying .map is not an array, but we do indeed make sure that it is (and anyhow it works on various other versions and devices). What are we missing here?

Some background for the curious

We have two arrays, one is an array of data sets we obtain from an API response, and the other one is a list of bookmarked data sets from a local DB on the device. We apply the map functionality on the API response array and check if a certain item from the data set is present in the database of favorites. Based on this, we change the color of the icon. According to our tester, he did also have internet access, but we anyhow monitor @react-native-community/netinfo version 2.0.0.

Related questions

B--rian
  • 5,578
  • 10
  • 38
  • 89
  • 1
    Are you doing something like this to really make sure `o` is an array: ```throw new Error(o instanceof Array ? 'o is an Array' : 'o is not an Array')```? – Jim Perris May 08 '19 at 16:38
  • It happens because `o` is not an iterable Object (Array), definitely, it is not related to the Android version. Also, the inserted links are not related to your issue. – AmerllicA Apr 08 '20 at 16:02
  • 2
    Please add your code – Rajan Apr 09 '20 at 04:52
  • Are you facing the similar issue during development time on Android Pie emulators or physical devices. Also can you find the actual code from where this error is happening, if so please share that – warl0ck Apr 14 '20 at 09:51
  • @warl0ck The issue with sharing the code is that I cannot reproduce the error with my development environment where everything works smoothly. I will still try to dig out the code rsn. – B--rian Apr 14 '20 at 10:01
  • ok, In that case could you check for the relevant code in your index.bundle file created while building the final APK, it will have simillar code then you might have some context as where this error is happening – warl0ck Apr 14 '20 at 10:41

2 Answers2

0

This would only happen if o is not an Array. You can check if o is an array using

Array.isArray(o);

More details about this here. It would be safe to rule out the possibility that it is a Android 9 specific issue. I have a Android 9 device and have tested map numerous times. If you are able to consistently reproduce only on Android 9, there is a chance there is something else into play when running on it. I'd suggest checking array existence using above^ method before you run a map on it.

Other options:

  1. If you need to change the icon color of certain elements from your array, and just need those elements, you can use .find() if for single element and .filter() for multiple elements.
  2. If you need the entire array as response, alternatives to .map() would be .forEach() and for of loops.

Happy to update the answer with more specific answers if you can update the question with your exact code (Even if it is not reproducible on desktop browsers)

Bhargav Shah
  • 737
  • 1
  • 7
  • 15
0

As others have said, o is not an iterable object.

You need to check higher in the stack for the problem. If o is supposed to be an array returned by an API call then it is probable that the API call has failed, or the parsing of its response has failed.

robdonn
  • 250
  • 3
  • 11