Search code examples
node.jsvue.jsvuejs2vue-componentv-for

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


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
}

Solution

  • 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