Chris Pang
Chris Pang

Reputation: 33

NodeJS: Cannot read property 'length' of undefined

When I run node app.js in nodejs, the error message shows 'Cannot read property 'length' of undefined'. I have installed all relevant libraries (e.g. request) and have looked at different relevant posts. However, the issue still persists.

Please could you advise how I may fix it. Many thanks!

Windows 10 Home PC Visual Studio Code

//app.js
const geocode = require('./utils/geocode')
const forecast = require('./utils/forecast')

geocode('Boston', (error, data) => {
console.log('Error', error)
console.log('Data', data)
forecast(data.latitude, data.longitude, (error, data) => {
    console.log('Error', error)
    console.log('Data', data)
})


})

// forecast.js
const request = require('request')

const forecast = (latitude, longitude, callback) => {
const url = 
'https://api.darksky.net/forecast/9d1465c6f3bb7a6c71944bdd8548d026/' + 
latitude + ',' + longitude

request({ url: url, json: true }, (error, response) => {
    if (error) {
        callback('Unable to connect to weather service!', undefined)
    } else if (response.body.error) {
        callback('Unable to find location', undefined)
    } else {
        callback(undefined, response.body.daily.data[0].summary + ' It is 
currently ' + response.body.currently.temperature + ' degress out. There 
is a ' + response.body.currently.precipProbability + '% chance of rain.')
    }
})
}

module.exports = forecast


// geocode.js
const request = require('request')

const geocode = (address, callback) => {
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + 
address + '.json?access_token=pk.eyJ1IjoiYW5kcmV3bWVhZDEiLCJhIjoiY2pvOG8ybW90MDFhazNxcnJ4OTYydzJlOSJ9.njY7HvaalLEVhEOIghPTlw&limit=1'

 request({ url: url, json: true }, (error, response) => {
    if (error) {
        callback('Unable to connect to location services!', undefined)
     } else if (response.body.features.length === 0) {
        callback('Unable to find location. Try another search.', 
undefined)
    } else {
        callback(undefined, {
            latitude: response.body.features[0].center[0],
            longitude: response.body.features[0].center[1],
            location: response.body.features[0].place_name
        })
    }
})
}

module.exports = geocode

I expect to see Boston, its latitude and its longitude.

Upvotes: 1

Views: 2812

Answers (3)

Abdulrazzaq
Abdulrazzaq

Reputation: 95

else if (typeof response.body.features == "undefined")

try this

Upvotes: 0

Kitani Islam
Kitani Islam

Reputation: 144

Its seems like the API return NULL ,so there's no "feature" in your response body in this case u have to check if you are really getting a response "response.body.features" then check the length . so your condition be come in your else statement :

if (response.body.features && response.body.features.length === 0) {}

Upvotes: 0

megna
megna

Reputation: 11

Before checking the features length, you can also check if features exists:

else if (response.body.features && response.body.features.length === 0) {

Upvotes: 1

Related Questions