node.js - How to retrieve data in nodejs from post api with vuejs -
i trying write post api in nodejs , using vuejs front end frame work , using vue-resource access apis . submit function on front end side.
validatebeforesubmit() { var self = this; this.$validator.validateall().then(result => { if (result) { self.data = self.name; console.log(this.data) this.$http.post('/api/add_level',self.data).then(function(res){ this.status = res.body console.log(this.status); this.largemodal=false; // eslint-disable-next-line alert('from submitted!'); }) return; } this code on server side.
app.post('/api/add_level',function(req,res,err,data){ console.log(data); })
you having varible var self = this pointing correct vue instancre , why using this again in callback of vue-resource's post request
use self every when in method wherever want o reference vue instance. possible due concept of closures.
so change code to:
this.$validator.validateall().then(function(result) { if (result) { self.data = self.name; console.log(this.data) this.$http.post('/api/add_level',self.data).then(function(res){ self.status = res.body console.log(self.status); self.largemodal=false; // eslint-disable-next-line alert('from submitted!'); }) return; } note
i recommend use normal function syntax , make use of closures or use arrow function bind this lexically. not mix them , use simultaneously
Comments
Post a Comment