如何解决:“无法读取未定义的属性'then'”

时间:2019-01-08 12:52:59

标签: javascript node.js

当我尝试从函数中获取响应时,我遇到了问题,我只是开始使用Promises,所以也许我做错了什么。 启动我的应用程序时,出现错误:Cannot read property 'then' of undefined。 我做错了什么? 在我尝试更改应用程序逻辑后,直到我开始使用Promises之前,一切都运行良好。 我的主要文件如下:

/app.js

const request = require('request');
const yargs = require('yargs');

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

const argv = yargs
    .options({
        a: {
            demand: true,
            alias: 'address',
            string: true,
            describe: 'Address to fetch weather for'
        }
    })
    .help()
    .alias('help', 'h')
    .argv;

geocode.geocodeAddress(argv.a)
.then((res) => {
    console.log(res);
},
(error) => {
    console.log(error);
})

我的地理编码文件:

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

const geocodeAddress = (address) => {
    const encodedAddress = encodeURIComponent(address);

    request({
        url: `https://maps.googleapis.com/maps/api/geocode/json?key=myKey&address=${encodedAddress}`,
        json: true
    }, (err, res, body) => {
        return new Promise((resolve, reject) => {
            if(err){
                reject('Unable to connect to Google servers.')
            } else if (body.status === 'ZERO_RESULTS') {
                reject('Unable find any address.')
            } else if (body.status === 'OK') {
                const lat = body.results[0].geometry.location.lat;
                const lon = body.results[0].geometry.location.lng;
                const adrs = body.results[0].formatted_address;
                const answer = {
                    lat: lat,
                    lon: lon,
                    address: adrs
                };
                resolve(answer);
            }
        })
    })
};

module.exports.geocodeAddress = geocodeAddress;

2 个答案:

答案 0 :(得分:1)

这可能是因为您的geocodeAddress是常量而不是异步函数。

盲目尝试:

async geocodeAddress (address) {

就像@liam说的那样,尝试:

return request({...})

答案 1 :(得分:1)

为我解决的代码:

const request = require('request');

const geocodeAddress = (address) => {
    const encodedAddress = encodeURIComponent(address);

    return new Promise((resolve, reject) => { 
        request({
        url: `https://maps.googleapis.com/maps/api/geocode/json?key=myKey&address=${encodedAddress}`,
        json: true
    }, (err, res, body) => {
            if(err){
                reject('Unable to connect to Google servers.')
            } else if (body.status === 'ZERO_RESULTS') {
                reject('Unable find any address.')
            } else if (body.status === 'OK') {
                const lat = body.results[0].geometry.location.lat;
                const lon = body.results[0].geometry.location.lng;
                const adrs = body.results[0].formatted_address;
                const answer = {
                    lat: lat,
                    lon: lon,
                    address: adrs
                };
                resolve(answer);
            }
        })
    })
};

module.exports.geocodeAddress = geocodeAddress;