Error in v-on handler (Promise/async): “TypeError: Cannot read property 'data' of undefined” // undefined

谁说胖子不能爱 提交于 2021-01-28 05:25:34

问题


I am trying to push data from a v-text-field onto a json file. When i tried it on Postman it worked so I'm guessing the error is coming from the client side

product.vue

<v-container>
   <v-row>
      <v-col>
         <v-text-field
            label="440"
            v-model="onetext"
            ></v-text-field>
         <v-text-field
            label="Card Type"
            v-model="twotext"
            ></v-text-field>
         <v-text-field
            label="Card Type"
            v-model="threetext"
            ></v-text-field>
         <v-text-field
            label="Card Type"
            v-model="fourtext"
            ></v-text-field>
         <v-text-field
            label="Card Type"
            v-model="fivetext"
            ></v-text-field>
         <v-text-field
            label="Card Type"
            v-model="sixtext"
            ></v-text-field>
         <v-text-field
            label="Card Type"
            v-model="seventext"
            ></v-text-field>
      </v-col>
   </v-row>
</v-container>
</v-card-text>
<v-card-actions>
   <v-spacer></v-spacer>
   <v-btn color="primary"  @click="dialog = false">Close</v-btn>
   <v-btn color="primary"  @click="create">Save</v-btn>
</v-card-actions>
import ProductService from '@/services/ProductService'
export default {
  components: {},
  data() {
    return {
      dialog: false,
      product: null,
      onetext: null,
      twotext: null,
      threetext: null,
      fourtext: null,
      fivetext: null,
      sixtext: null,
      seventext: null
    }
  },
  async mounted() {},
  methods: {
    async create() {
      try {
        await ProductService.create(this.onetext, this.twotext, this.threetext, this.fourtext, this.fivetext, this.sixtext, this.seventext)
      } catch (error) {
        this.error = error.response.data.message
      }
    }
  }
}

ProductService.js

export default {
    create(){
        return Api().post('/product', one, two, three, four, five, six, seven);
    }
}

ProductRouter:

router.post("/", function(req, res){
    try{
        const fileName = path.resolve("server",'../product.json');
        const file = require(fileName);

        const product = {
            101: req.params.one,
            201: req.params.two,
            420: req.params.three,
            440: req.params.four,
            444: req.params.five,
            451: req.params.six,
            452: req.params.seven
        }

        fs.writeFile(fileName, JSON.stringify(product, null, 2), function(err){
            if (err){
                return console.log(err);
            }
            console.log("the file was saved");
            res.status(200).json(product);
        });
    } catch(err){
        res.status(500).json({
            message: "Error writing to file",
            error: err
        });
    }
})

ERR: Error in v-on handler (Promise/async): "TypeError: Cannot read property 'data' of undefined"

  • Why am i getting this error and is there anything i could do to fix this?

EDIT: Expected product.json file

{
  "101": 1.9,
  "201": 2.18,
  "420": 4.1,
  "440": 9.2,
  "444": 11.16,
  "451": 122.12,
  "452": 11.9
}

回答1:


Solution:

Your create function is using arguments that were not passed. Change it to:

export default {
    create(one, two, three, four, five, six, seven){ // <-- add arguments
        return Api().post('/product', one, two, three, four, five, six, seven);
    }
}

Improvement:

Simplify everything:

1) Move all form model data into one data object formData:

return {
  // ...
  formData: {
    onetext: null,
    twotext: null,
    ...
  }
}

2) In the template, instead of hard-coding the list of v-text-field, use v-for to loop over that formData object:

<v-text-field v-for="(value, key) in formData" :key="key"
              label="Card Type" v-model="formData[key]"
></v-text-field>

3) Pass the formData object in the create method instead of multiple arguments:

await ProductService.create(this.formData);

4) Change ProductService.create, Api().post, and router.post to use/pass that object:

create(formData){ // <-- Accepts an object now
    return Api().post('/product', formData); // <-- Passes the object
}

Here's a demo



来源:https://stackoverflow.com/questions/61315257/error-in-v-on-handler-promise-async-typeerror-cannot-read-property-data-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!