如何将新对象添加到观察者类型-javascprit

时间:2019-02-18 05:56:33

标签: javascript vue.js

我想向我的数组对象添加新的key/value,但出现此错误:

Uncaught TypeError: Cannot set property 'english_province' of undefined

enter image description here

addresses[0].english_province        = "test"
or
addresses[0]['english_province']    = "test"

我的功能:

function prepationAddresses(){
            var addresses = app.vueList;
            console.log('_addresses',addresses)
            console.log('_addresses.length',addresses.length)
            console.log('_addresses.0',addresses[0])
            for(var i = 0 ; i < addresses.length ; ++i){
                var item = addresses[i];
                var mGeocode = null
                getGeocode(item.x,item.y).then(function (_mGeocode){
                    console.log('_mGeocode',_mGeocode)
                    mGeocode = _mGeocode
                });
                setTimeout(() => {
                    console.log('mGeocode',mGeocode.results[0])
                    console.log('_addresses',addresses)
                    console.log('_addresses item',item)

                    //console.log('mGeocode',mGeocode.results[0].address_components[4])
                    addresses[i].english_province    = mGeocode.results[4].address_components[1].long_name
                    addresses[i].english_city        = mGeocode.results[4].address_components[0].long_name   

                }, 1000);

            }

            setTimeout(() => {
              //  submitNewRequest(addresses)
              console.log(addresses);
            }, 5000);
        }

0 个答案:

没有答案