最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

NodeJS:无法读取未定义的属性'length'

IT培训 admin 6浏览 0评论

NodeJS:无法读取未定义的属性'length'

[当我在nodejs中运行node app.js时,错误消息显示'无法读取未定义的属性'length'。我已经安装了所有相关的库(例如,请求),并查看了不同的相关帖子。但是,问题仍然存在。

[请您告知我如何解决。非常感谢!

Windows 10家用PCVisual Studio代码

//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 = 
'/' + 
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 = '.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

我希望看到波士顿,它的纬度和经度。

回答如下:

在检查features长度之前,您还可以检查features是否存在:

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

NodeJS:无法读取未定义的属性'length'

[当我在nodejs中运行node app.js时,错误消息显示'无法读取未定义的属性'length'。我已经安装了所有相关的库(例如,请求),并查看了不同的相关帖子。但是,问题仍然存在。

[请您告知我如何解决。非常感谢!

Windows 10家用PCVisual Studio代码

//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 = 
'/' + 
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 = '.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

我希望看到波士顿,它的纬度和经度。

回答如下:

在检查features长度之前,您还可以检查features是否存在:

else if (response.body.features && response.body.features.length === 0) {
发布评论

评论列表 (0)

  1. 暂无评论