javascript [Vue警告]:v-on处理程序中出错:"类型错误:无法读取未定义的属性

ztmd8pv5  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(139)

尝试将数据发布到集合(使用Vue(2.6.11)、Vuetify(2.4.0)、Vuex(3.6.2)、Vue-router(3.5.1)、Axios),但出现此错误。无法修复,不确定为什么无法工作。
添加项目窗体(AddItem.vue):

<v-form ref="form" v-model="valid">
                    <v-text-field name="title" v-model="form.title" label="title" required>
                    </v-text-field>

                    <v-text-field name="description" v-model="form.description" label="Item Description" required>
                    </v-text-field>

                    <v-select name="categoryID" v-model="form.category" :items="categories" item-text="name" item-value="_id" label="Category" required>
                    </v-select>

                    <v-select name="qualityID" v-model="form.quality" :items="qualities" item-text="name" item-value="_id" label="Quality" required>
                    </v-select>

                    <v-text-field name="price" v-model="form.price" label="Price" required>
                    </v-text-field>

                    <v-file-input name="photo" v-model="form.photo" multiple label="Item photo(s)">
                    </v-file-input>

                    <v-btn rounded text :disabled="!valid" @click="addItem()">
                                Add
                            </v-btn>

                    <v-btn @click="reset">
                        Reset Form
                    </v-btn>
                </v-form>

<script>
    import GoBack from '@/components/GoBack'

    export default {
        name: "addItem",
        data: () => ({
            form: {
                title: "",
                description: "",
                category: "",
                quality: "",
                price: ""
            },
            categories: [
                { _id: "620a6acaff3f5cebc8370121", name: 'Food' },
                { _id: "620a6ae3ff3f5cebc8370123", name: 'Clothes' },
                { _id: "620a6af1ff3f5cebc8370125", name: 'Furniture' },
                { _id: "620a6b04ff3f5cebc8370127", name: 'Electronics' },
                { _id: "620a7a0fded499a220f386d1", name: 'Tools' },
                { _id: "620a7ca7178dada11844dbad", name: 'Toys' }
            ],
        }),
        methods: {
            addItem() {
                if (this.$refs.form.validate()) {
                    this.$store.dispatch('addItem', this.form)
                }
            }
        }
    };
</script>

商店.js:

import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/config'
import router from '@/router'

Vue.use(Vuex)

export default new Vuex.Store({
  
  actions: {
    addItem() {
      axios
        .post(`/items`, {
          title: this.form.title,
          description: this.form.description,
          categoryID: this.form.category,
          qualityID: this.form.quality,
          price: this.form.price
        })
        .then(response => {
          console.log(response.data)
        })
        .catch(error => {
          console.log(error)
          console.log(error.response.data.message)
          router.push('/items').catch(() => {});
        })
    }
  }
})

错误:

错误中的“title”专门引用了“this.store.title”中的“title”(例如,如果我将其更改为“this.store.title1”,错误将开始引用“title 1”),以为我不知道它有什么问题,也找不到任何解决方案。如果有人知道我如何解决这个问题,我将非常感谢,如果有任何更多的信息我可以提供,请让我知道,谢谢你的时间。

nbnkbykc

nbnkbykc1#

在您的应用商店中,您试图使用未定义的.form。您必须使用一个参数,而不是使用.form。您已经在this.$store.dispatch('addItem', this.form)处将一个参数传递给您的操作,因此在此端无需执行任何操作,但您没有使用它。

import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/config'
import router from '@/router'

Vue.use(Vuex)

export default new Vuex.Store({
  
  actions: {
    addItem({},form) {
      axios
        .post(`/items`, {
          title: form.title,
          description: form.description,
          categoryID: form.category,
          qualityID: form.quality,
          price: form.price
        })
        .then(response => {
          console.log(response.data)
        })
        .catch(error => {
          console.log(error)
          console.log(error.response.data.message)
          router.push('/items').catch(() => {});
        })
    }
  }
})
v8wbuo2f

v8wbuo2f2#

对不起,我也有这样的问题。
错误[Vue警告]:事件"selectRole"的处理程序无效:未定义13:38:41
发现于

---> <Select>
       <ASelect>
         <ACol>
           <ARow>
             <AFormItem>
               <AForm>
                 <Pages/register.vue> at pages/register.vue
                   <Nuxt>
                     <Layouts/userLogin.vue> at layouts/userLogin.vue
                       <Root>

相关问题