Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

element-ui 自定义表单验证 请求后端接口报错 callback is not a function?

在浏览器中打断点发现会执行两次这个函数,第一次验证的时候没有任何问题,正常请求接口,但是不知道为什么会执行第二次。

import { checkCode } from "@/api/sales/globalWares";

var checkCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入编码'));
      } else {
        checkCode({ waresCode: value }).then(res => {
          if (res.data > 0) {
            callback(new Error('编码重复'));
          } else {
            callback();
          }
        });
      }
    }
rules: {
        waresCode: [
          { required: true, validator: checkCode, trigger: 'blur' }
        ],
        shortName: [
          { required: true, message: "商品简称不能为空", trigger: "blur" }
        ]
      },
vue.runtime.esm.js:1888 TypeError: callback is not a function
    at checkCode (index.vue?76f2:171)
    at Object.checkCode [as validator] (index.vue?76f2:173)
    at index.js:216
    at next (util.js:114)
    at asyncSerialArray (util.js:120)
    at util.js:154
    at Array.forEach (<anonymous>)
    at asyncMap (util.js:151)
    at Schema.validate (index.js:141)
    at VueComponent.validate (element-ui.common.js:23501)
logError @ vue.runtime.esm.js:1888
globalHandleError @ vue.runtime.esm.js:1879
handleError @ vue.runtime.esm.js:1839
invokeWithErrorHandling @ vue.runtime.esm.js:1862
Vue.$emit @ vue.runtime.esm.js:3882
dispatch @ emitter.js:29
handleBlur @ element-ui.common.js:4811
invokeWithErrorHandling @ vue.runtime.esm.js:1854
invoker @ vue.runtime.esm.js:2179
original._wrapper @ vue.runtime.esm.js:6911

验证部分代码就是这样的,不知道有没有哪位大佬能帮忙解答一下。。。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

导入的函数checkCode和验证用的函数重名,导致他调用到自己,循环调用


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...