简体   繁体   English

YUP 验证期间出错“无法读取未定义的属性‘长度’”

[英]Error during YUP validation “Cannot read property 'length' of undefined”

I got the following code block:我得到以下代码块:

 const schema = useMemo(
    () =>
      yup.object().shape({
        name: yup
          .string()
          .trim()
          .required("Missing name")
          .max(40, "Too long"),
        template: yup
          .string()
          .trim()
          .max(2000, "Too long")
          .matches(/^https?:\/\//, "Invalid protocol")
          .required("Missing template")
          .test("variablesUsage", "Missing vars", function (
            value
          ) {
            return vars.some(v => value.includes(`{${v}}`))
          }),
        enabled: yup.boolean(),
      }),
    [message, vars]
  )

The problem is that everytime i type something in "name" field, I get the following error in console: "Uncaught (in promise) TypeError: Cannot read property 'length' of undefined"问题是每次我在“名称”字段中输入内容时,都会在控制台中收到以下错误:“未捕获(承诺中)TypeError:无法读取未定义的属性'长度'”

It works fine if I remove the.test from "template", however I could not find the root cause of this error.如果我从“模板”中删除 .test,它工作正常,但是我找不到这个错误的根本原因。

Basically, in.test, there are some specific keywords, at least 1 of these keywords must be present in "template"基本上,在.test中,有一些特定的关键字,这些关键字中至少有1个必须出现在“模板”中

It seems like the error was caused by this line似乎错误是由这一行引起的

return vars.some(v => value.includes(`{${v}}`))

I have added the following line:我添加了以下行:

if (value == null) {return false;}

And it is working.它正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM