首页 > 解决方案 > 响应数据未定义

问题描述

所以我使用 Vuelidate 来验证我的表单。我尝试使用插件文档创建自己的验证助手,但是当我尝试使用 axios 请求时,它似乎给了我“无法读取未定义的属性 '__isVuelidateAsyncVm'”错误。但是,如果我只是返回 true 或者如果我检查输入是否等于值“用户名”,它会按预期工作。

我已经尝试了很多事情。我目前的尝试是:这给了我错误:Cannot read property '__isVuelidateAsyncVm' of undefined

export function isUser(value) {
  if(value === "") return true;
  this.$http.get('/api/user/check/'+ value).then((response) => { // this.$http = Axios.get
    console.log(response.data);
    return response.data; // returns true or false
  })
}

这个不会给出错误和“工作”,但总是返回为 false ,这会迫使错误出现。

export function isUser(value) {
  var isUser;
  this.$http.get('/api/user/check/'+ value).then((response) => {
    console.log(response.data) // returns true if value matches a username
    isUser = response.data
  });
  console.log(isUser); // Returns undefined
  return isUser ? true : false
}

我也尝试过使用惰性函数。v-model.trim.lazy="$v.username.$model">但它也没有改变任何东西,我更希望它在没有 .lazy 的情况下即时执行

标签: javascriptapivue.jsvuejs2

解决方案


推荐阅读