0

I have been having this issue lately and I am not sure how to resolve it. I have two applications running at the same time. The first one is fetching APIs from vessel-finder and finding only a specific number of boats. The second application is the user interface used to visualize data from the fetch, specifically latitude and longitude of boats.

The error is below

map

This is the answer of the API:

[  
    {  
        "AIS":{  
            "MMSI":227441980,
            "TIMESTAMP":"2017-08-11 11:17:37 UTC",
            "LATITUDE":46.1459,
            "LONGITUDE":-1.16631,
            "COURSE":360.0,
            "SPEED":0.0,
            "HEADING":511,
            "NAVSTAT":1,            
            "IMO":0,
            "NAME":"CLEMENTINE",
            "CALLSIGN":"FJVK",
            "TYPE":60,
            "A":0,
            "B":0,
            "C":0,
            "D":0,
            "DRAUGHT":0.0,
            "DESTINATION":"",
            "ETA_AIS":"00-00 00:00",
            "ETA":"",
            "SRC":"TER",
            "ZONE": "North Sea",
            "ECA": true      
        }
    }
]

This is the code I am using and where the problem seems to be:

ShipTracker.js

import React from 'react';
import { Table } from 'reactstrap';

const shipCompanyMap = {
    MICHIGAN: 'DONJON',
    ATLANTIC SALVOR': 'DONJON',
    STUYVESANT: 'DUTRA'
};

const Ship = ({ ship, logoMap, logoClick }) => {
const shipName = ship.AIS.NAME;
const company = shipCompanyMap[shipName];
const img = logoMap[company && company.split(' ').join('').toUpperCase()];
return (
    <div onClick={(event) => logoClick(event, ship)}>
        {/* Render shipImage image */}
        <img src={img} alt="Logo" />
    </div>
);
};
export { Ship };

const ShipTracker = ({ ships, setActiveShip }) => {
console.log('These are the ships: ', { ships });

return (
    <div className="ship-tracker">
        <Table className="flags-table" responsive hover>
            <thead>
                <tr>
                    <th>#</th>
                    <th>MMSI</th>
                    <th>TIMESTAMP</th>
                    <th>LATITUDE</th>
                    <th>LONGITUDE</th>
                    <th>COURSE</th>
                    <th>SPEED</th>
                    <th>HEADING</th>
                    <th>NAVSTAT</th>
                    <th>IMO</th>
                    <th>NAME</th>
                    <th>CALLSIGN</th>
                </tr>
            </thead>
            <tbody>
                {ships.map((ship, index) => {
                    // const { IMO, NAME, CALLSIGN, HEADING, SOG, MMSI, LONGITUDE, LATITUDE } = ship;
                    // const cells = [ NAME, CALLSIGN, HEADING, SOG, IMO, MMSI, LONGITUDE, LATITUDE ];

                    const {
                        MMSI,
                        TIMESTAMP,
                        LATITUDE,
                        LONGITUDE,
                        COURSE,
                        SPEED,
                        HEADING,
                        NAVSTAT,
                        IMO,
                        NAME,
                        CALLSIGN
                    } = ship;

                    const cells = [
                        MMSI,
                        TIMESTAMP,
                        LATITUDE,
                        LONGITUDE,
                        COURSE,
                        SPEED,
                        HEADING,
                        NAVSTAT,
                        IMO,
                        NAME,
                        CALLSIGN
                    ];

                    return (
                        <tr
                            onClick={() => setActiveShip(ship.AIS.NAME, ship.AIS.LATITUDE, ship.AIS.LONGITUDE)}
                            key={index}
                        >
                            <th scope="row">{index}</th>
                            {cells.map((cell) => <td key={ship.AIS.MMSI}>{cell}</td>)}
                        </tr>
                    );
                })}
            </tbody>
        </Table>
    </div>
);
};

export default ShipTracker;

Here on GoogleMap.js is where I render the images on google-map:

render() {
    return (
        <div className="google-map">
            <GoogleMapReact
                bootstrapURLKeys={{ key: 'My_KEY' }}
                center={{
                    lat: this.props.activeShip ? this.props.activeShip.latitude : 37.99,
                    lng: this.props.activeShip ? this.props.activeShip.longitude : -97.31
                }}
                zoom={5.5}
            >

                {/* Rendering all the markers here */}
                {this.state.ships.map((ship) => (
                    <Ship
                        ship={ship}
                        key={ship.AIS.MMSI}
                        lat={ship.AIS.LATITUDE}
                        lng={ship.AIS.LONGITUDE}
                        logoMap={this.state.logoMap}
                        logoClick={this.handleMarkerClick}
                    />
                ))}
            </GoogleMapReact>
        </div>
    );
}

What I tried so far:

1) I tried to modify the how the cells in the Table are passed/read hoping to fix at the same time the variable.map is not a function issue. Below the additional trial I tried but also that did not work:

const ShipTracker = ({ ships, setActiveShip }) => {
    console.log('These are the ships: ', { ships });

    return (
        <div className="ship-tracker">
            <Table className="flags-table" responsive hover>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>MMSI</th>
                        <th>TIMESTAMP</th>
                        <th>LATITUDE</th>
                        <th>LONGITUDE</th>
                        <th>COURSE</th>
                        <th>SPEED</th>
                        <th>HEADING</th>
                        <th>NAVSTAT</th>
                        <th>IMO</th>
                        <th>NAME</th>
                        <th>CALLSIGN</th>
                    </tr>
                </thead>
                <tbody>
                    {ships.map((ship, index) => {
                        // const { IMO, NAME, CALLSIGN, HEADING, SOG, MMSI, LONGITUDE, LATITUDE } = ship;
                        // const cells = [ NAME, CALLSIGN, HEADING, SOG, IMO, MMSI, LONGITUDE, LATITUDE ];

                        // const {
                        //  MMSI,
                        //  TIMESTAMP,
                        //  LATITUDE,
                        //  LONGITUDE,
                        //  COURSE,
                        //  SPEED,
                        //  HEADING,
                        //  NAVSTAT,
                        //  IMO,
                        //  NAME,
                        //  CALLSIGN
                        // } = ship;

                        const cells = [
                            ship.AIS.MMSI,
                            ship.AIS.TIMESTAMP,
                            ship.AIS.LATITUDE,
                            ship.AIS.LONGITUDE,
                            ship.AIS.COURSE,
                            ship.AIS.SPEED,
                            ship.AIS.HEADING,
                            ship.AIS.NAVSTAT,
                            ship.AIS.IMO,
                            ship.AIS.NAME,
                            ship.AIS.CALLSIGN
                        ];

                        return (
                            <tr
                                onClick={() => setActiveShip(ship.AIS.NAME, ship.AIS.LATITUDE, ship.AIS.LONGITUDE)}
                                key={index}
                            >
                                <th scope="row">{index}</th>
                                {cells.map((cell) => <td key={ship.AIS.MMSI}>{cell}</td>)}
                            </tr>
                        );
                    })}
                </tbody>
            </Table>
        </div>
    );

2) I also came across this source and this other source, this one and this too.

I read this also. However, neither of these has helped me fix the problem.

What am I doing wrong that is preventing me from correctly compiling the project? Thank you very much for pointing to the right direction for solving this issue.

Emanuele
  • 2,194
  • 6
  • 32
  • 71

2 Answers2

0

Please check out your result, prototype has map function or not.

You can check the available function on that result by checking proto property of that object.

You can check it in console like example:-

let a = [1,2,3]

(3) [1, 2, 3]
0: 1
1: 2
2: 3
length: 3
__proto__: Array(0)
length: 0
constructor: ƒ Array()
concat: ƒ concat()
copyWithin: ƒ copyWithin()
fill: ƒ fill()
find: ƒ find()
findIndex: ƒ findIndex()
lastIndexOf: ƒ lastIndexOf()
pop: ƒ pop()
push: ƒ push()
reverse: ƒ reverse()
shift: ƒ shift()
unshift: ƒ unshift()
slice: ƒ slice()
sort: ƒ sort()
splice: ƒ splice()
includes: ƒ includes()
indexOf: ƒ indexOf()
join: ƒ join()
keys: ƒ keys()
entries: ƒ entries()
values: ƒ values()
forEach: ƒ forEach()
filter: ƒ filter()
flat: ƒ flat()
flatMap: ƒ flatMap()
map: ƒ map()
every: ƒ every()
some: ƒ some()
reduce: ƒ reduce()
reduceRight: ƒ reduceRight()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
Symbol(Symbol.iterator): ƒ values()
Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}
__proto__:
constructor: ƒ Object()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
hasOwnProperty: ƒ hasOwnProperty()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toString: ƒ toString()
valueOf: ƒ valueOf()
toLocaleString: ƒ toLocaleString()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
DHRUV GUPTA
  • 2,000
  • 1
  • 15
  • 24
  • Thanks for reading the question. I checked and [this](https://i.imgur.com/kOgn3oI.png) is a print screen of what I see. The array seems empty. – Emanuele Mar 11 '20 at 13:19
  • can you see in your screenshot this is an object, hence you need to use result['ships'] and object doesnt have map function in its prototype. – DHRUV GUPTA Mar 12 '20 at 05:34
  • Thanks for the comment. How do I fix that? Could you please add some code to your answer so that I can see the fixes in the code I posted? That would be very helpful! :) – Emanuele Mar 12 '20 at 11:16
  • just use result['ships'] in your code it should fix. – DHRUV GUPTA Mar 12 '20 at 11:23
  • Sorry :) how should I do that? – Emanuele Mar 12 '20 at 11:25
0

From your code, it is not clear on typeof "ships". map function operates on arrays only. So, check if ships is of type array before using map function on it.

user2063635
  • 214
  • 1
  • 10
  • Thank you for reading the question. How do I do that? The strange thing is that as soon as I launch the application (the ui) for like one second I see everything and than it crashes. It crashes with [this](https://i.imgur.com/OEACn38.png) additional error. – Emanuele Mar 11 '20 at 13:40
  • in file GoogleMap.js, in render funtcion, add a statement `console.log("ships ", this.state.ships);` comment the component GoogleMapReact – user2063635 Mar 11 '20 at 13:48
  • Ok that additional error is solved. I was making too many requests per minute and it was going on time out. Thank you! :) However I still have the same error that is coming from the `ShipTracke.js`. `console.log('These are the ships: ', { ships });` still returns an empty array – Emanuele Mar 11 '20 at 14:44
  • You can see that in [this print screen](https://i.imgur.com/KVf3zs5.png) the table under `google-map` is not filled with rows, despite I get all the ships. Plus I don't know why the **ShipTracker.js** files seems not to accept something. – Emanuele Mar 11 '20 at 14:51
  • Is it because I am misinterpreting the API response I wrote at the beginning of the question? – Emanuele Mar 11 '20 at 14:57
  • looks like ShipTracker.js is receiving ships as props and it is empty too. Debug on the component which is rendering this ShipTracker.js component. – user2063635 Mar 11 '20 at 16:42