简体   繁体   English

获取Vue警告TypeError:无法读取未定义的属性,但存在数据

[英]Getting a Vue warning TypeError: Cannot read property of undefined but the data is present

I am getting the following errors: 我收到以下错误:

[Vue warn]: Error in nextTick: "TypeError: Cannot read property 'itemName' of undefined"

found in

---> <AssetEditForm> at src\components\AssetEditForm.vue
       <AppSection> at src\components\AppSection.vue
         <MyAssets> at src\views\Assets.vue
           <App> at src\App.vue
             <Root>

and

vue.runtime.esm.js?2b0e:1737 TypeError: Cannot read property 'itemName' of undefined
at VueComponent.itemName (AssetEditForm.vue?6bb7:218)
at Watcher.get (vue.runtime.esm.js?2b0e:3138)
at Watcher.evaluate (vue.runtime.esm.js?2b0e:3245)
at VueComponent.computedGetter [as itemName] (vue.runtime.esm.js?2b0e:3503)
at VueComponent.eval (AssetEditForm.vue?6bb7:104)
at Array.eval (vue.runtime.esm.js?2b0e:1833)
at flushCallbacks (vue.runtime.esm.js?2b0e:1754)

NB NB

  • I am using the same code in very similar components and not getting these errors 我在非常相似的组件中使用相同的代码,但未收到这些错误
  • All items in Created() throw this warning, ie if I remove this.itemName then this.ownership will trigger the warning, and so on Created()中的所有项目都会引发此警告,即如果我删除this.itemNamethis.ownership将触发该警告,依此类推
  • Even though these errors are present, the data is available in Vue dev tools and displays as expected in the browser 即使存在这些错误,数据仍可在Vue开发工具中使用并按预期在浏览器中显示
  • The errors are only present when I edit a dynamically created item. 仅当我编辑动态创建的项目时才出现错误。 For instance items pulled in via an API do not trigger this warning when editing them 例如,通过API提取的项目在编辑时不会触发此警告

AssetEditForm AssetEditForm

props: {
    myAssets:{},
    assetId:{
        type: String
    },
},data: function () {
    return {
        isShown: this.showForm,
        myAssetId: this.assetId,
        assetName: '',
        assetOwnership: '',
        assetOwnershipPercentage: '',
        assetEstimatedValue: '',
        typePlural: this.type + '\'s',
        typeLowerCase: this.type.toLowerCase(),
        // assetIdIs: '',
    }
},
created: function () {
    this.$nextTick(function () {
        console.log('created');
        this.assetName = this.itemName;
        this.assetOwnership = this.ownership;
        this.assetOwnershipPercentage = this.ownershipPercentage;
        this.assetEstimatedValue = this.estimatedValue;
    })
},
computed: {
    getAssetId: function() {
        const myInfo = this.myAssets;
        return myInfo.find(x => x.id == this.assetId);
    },
    itemName: function() {
        return this.getAssetId.itemName
    },
    ownership: function() {
        return this.getAssetId.ownership
                },
    ownershipPercentage: function() {
        return this.getAssetId.ownershipPercentage
    },
    estimatedValue: function() {
        return this.getAssetId.estimatedValue
    },
},

The problem might be the computed property for itemName . 问题可能是itemName的计算属性。 If this.myAssets is empty or does not contain the correct item find() in getAssetId returns undefined ( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find ). 如果this.myAssets为空或在getAssetId中不包含正确的项find()getAssetId返回未定义( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find )。 getAssetId is used in the computed property for itemName where you are trying access itemName of undefined. 在试图访问未定义itemNameitemName的计算属性中,使用了getAssetId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性“ present” - TypeError: Cannot read property 'present' of undefined FIREBASE警告:TypeError:无法读取未定义的属性“ then” - FIREBASE WARNING: TypeError: Cannot read property 'then' of undefined 存在数据时出现错误“无法读取未定义的属性‘url’” - Getting error "Cannot read property 'url' of undefined" when data is present Vue TypeError:无法读取未定义的属性“loggedIn” - Vue TypeError: Cannot read property 'loggedIn' of undefined TypeError:无法读取 Vue 中未定义的属性“_modulesNamespaceMap” - TypeError: Cannot read property '_modulesNamespaceMap' of undefined in Vue Nativescript Vue TypeError:无法读取未定义的属性“ 0” - Nativescript Vue TypeError: Cannot read property '0' of undefined Vue TypeError:无法读取未定义的属性“_wrapper” - Vue TypeError: Cannot read property '_wrapper' of undefined ×获取TypeError:无法读取未定义的属性“ map” - × Getting a TypeError: Cannot read property 'map' of undefined 得到这个类型错误:无法读取未定义的属性“地图” - getting this TypeError: Cannot read property 'map' of undefined 拦截器TypeError:无法读取未定义的属性“数据” - interceptors TypeError: Cannot read property 'data' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM