ReactJS выброс `TypeError: Невозможно прочитать свойство 'latLng' of undefined` - PullRequest
1 голос
/ 17 июня 2020

Я построил визуализатор лодки, используя этот API . После запроса API я могу получить ответ json с интересующими меня судами и записать эту информацию API в базу данных MongoDB.

Проблема: Вроде все работает, но вдруг приложение перестает работать, выдает странную ошибку внизу. Теперь я много исследовал возможную причину:

TypeError: Cannot read property 'latLng' of undefined

то, что я не понимаю в ошибке, - это то, что указывает на свойство, которое я допускаю у меня есть: latLng и это я вижу только в модулях узлов node_modules/react/cjs/react.development.js:1149

В моем коде у меня нет latLng, но есть latlng (обратите внимание на строчную букву «l»), и я показал это в моем коде ниже async updateRequest() { ... }

Я также сделал снимок экрана для полноты картины. Вывод содержит странный вывод от node_modules/react/cjs/react.development.js:1149 и от интересующей части кода. Последняя часть ошибки - мой код BoatMap.updateRequest:

error

error2

сервер

var express = require('express');
var router = express.Router();
var axios = require('axios');
const NodeCache = require('node-cache');
const myCache = new NodeCache();

let hitCount = 0;

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

router.get('/hello', async function(req, res, next) {
    const allData = myCache.get('allData');

    if (!allData) {
        hitCount++;
        try {
            const { data } = await axios.get(
                'https://api.vesselfinder.com/vesselslist?userkey=KEY'
            );
            const { metaData, ships } = data;
            myCache.set('allData', data, 70);
            console.log(data + 'This is the data');
            res.send(data);
        } catch (error) {
            res.send(error);
            console.log(error);
        }
    }
    res.send(allData);
});

module.exports = router;

клиент

class BoatMap extends Component {
    constructor(props) {
        super(props);
        this.state = {
            ships: [],
            filteredShips: [],
            type: 'All',
            shipTypes: [],
            activeShipTypes: [],
            delayHandler: null,
            mapLoaded: false
        };
        this.updateRequest = this.updateRequest.bind(this);
    }

    async componentDidMount() {
        this.countDownInterval = setInterval(() => {
        }, 500);

        await this.updateRequest();

        this.updateInterval = setInterval(() => {
            this.updateRequest();
        }, 60 * 1000);
    }


    async updateRequest() {
        const url = 'http://localhost:3001/hello';
        const fetchingData = await fetch(url);
        const ships = await fetchingData.json();
        console.log('fetched ships', ships);

        if (JSON.stringify(ships) !== '{}') {
            if (this.previousTimeStamp === null) {
                this.previousTimeStamp = ships.reduce(function(obj, ship) {   
                    obj[ship.AIS.NAME] = ship.AIS.TIMESTAMP;
                    return obj;
                }, {});
            }

            this.setState({
                ships: ships,
                filteredShips: ships
            });

            this.props.callbackFromParent(ships);

            for (let ship of ships) {
                if (this.previousTimeStamp !== null) {
                    if (this.previousTimeStamp[ship.AIS.NAME] === ship.AIS.TIMESTAMP) {
                        this.previousTimeStamp[ship.AIS.NAME] = ship.AIS.TIMESTAMP;
                        console.log('Same timestamp: ', ship.AIS.NAME, ship.AIS.TIMESTAMP);
                        continue;
                    } else {
                        this.previousTimeStamp[ship.AIS.NAME] = ship.AIS.TIMESTAMP;
                    }
                }

                let _ship = {
                    // ships data ....
                };
                const requestOptions = {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(_ship)
                };
                await fetch('http://localhost:3001/users/vessles/map/latlng', requestOptions);
            }
        }
    }
}





render() {
    const noHoverOnShip = this.state.hoverOnActiveShip === null;
    // console.log("color", this.state.trajectoryColor);
    return (
        <div className="google-map">
            <GoogleMapReact
                // ships={this.state.ships}
                bootstrapURLKeys={{ key: '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}
                onGoogleApiLoaded={({ map, maps }) => {
                    this.map = map;
                    this.maps = maps;
                    // we need this setState to force the first mapcontrol render
                    this.setState({ mapControlShouldRender: true, mapLoaded: true });
                }}
            >
                {this.state.mapLoaded ? (
                    <div>
                        <Polyline
                            map={this.map}
                            maps={this.maps}
                            markers={this.state.trajectoryData}
                            lineColor={this.state.trajectoryColor}
                        />
                    </div>
                ) : (
                    ''
                )}

                {/* Rendering all the markers here */}
                    {Array.isArray(this.state.filteredShips) ? (
                        this.state.filteredShips.map((ship) => (
                            <Ship
                                ship={ship}
                                key={ship.AIS.MMSI}
                                lat={ship.AIS.LATITUDE}
                                lng={ship.AIS.LONGITUDE}
                                logoMap={this.state.logoMap}
                                logoClick={this.handleMarkerClick}
                                logoHoverOn={this.handleMarkerHoverOnShip}
                                logoHoverOff={this.handleMarkerHoverOffInfoWin}
                            />
                        ))
                    ) : (
                        'Loading...'
                    )}

                </GoogleMapReact>
        </div>
    );
}

Что у меня есть сделано до сих пор:

1) Я также наткнулся на этот источник , чтобы помочь мне решить проблему, но безуспешно.

2) Также я проконсультировался с этим другим источником , а также этим , но оба они не помогли мне выяснить, в чем может быть проблема.

3) Я углубился в проблему и нашел и этот источник .

4) Я читал и этот . Однако ни один из них не помог мне решить проблему.

5) Я также нашел этот источник очень полезным, но все еще без решения.

Спасибо за указание вправо направление решения данной проблемы.

1 Ответ

1 голос
/ 17 июня 2020

Я обнаружил ту же проблему здесь , связанную с google-map-react.

глядя на ваш код, ваш резервный вариант представляет собой обычную строку (внутри вашего GoogleMapReact компонента), и это будет вызывать ошибку, когда это должен быть компонент.

изменить: ваш компонент GoogleMapReact также не выглядит закрытым (возможно, пропущен из-за вставки кода)

Я бы использовал оператор && например:

 { this.state.mapLoaded && (
                    <div>
                        <Polyline
                            map={this.map}
                            maps={this.maps}
                            markers={this.state.trajectoryData}
                            lineColor={this.state.trajectoryColor}
                        />
                    </div>) 
}

для <Ship> logi c Я бы попробовал сначала обернуть <div>Loading...</div>, но я предполагаю, что google-map-react может не принять это. Если это не так, я бы использовал тот же подход с &&.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...