简体   繁体   中英

Uncaught TypeError: Cannot read property 'lat' of undefined React.JS

I have this code that takes the user's location, but when the user does not accept the permission, it sends the error of the question, how could I assign a value to it if the user left it empty?

const request = {
                method: 'POST',
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json"
                },
                body: JSON.stringify({
                    type: "sso",
                    ...state,
                    card: cifrado,
                    latitud: location.coordinates.lat,
                    longitud: location.coordinates.lng,
                    aceptacion: location.aceptacion
                }),
                latitud: location.coordinates.lat,
                longitud: location.coordinates.lng,
                aceptacion: location.aceptacion
            }
latitud: location?.coordinates.lat || '',
longitud: location?.coordinates.lng || '',

or

 body: JSON.stringify({
                type: "sso",
                ...state,
                card: cifrado,
                ...(location.cooridantes ? {
                  latitud: location.coordinates.lat,
                  longitud: location.coordinates.lng    
                 } : {}),
                aceptacion: location.aceptacion
            })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM