Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 福米克&x2B;是的,访问嵌套值时出错_Node.js_Reactjs_Formik_Yup - Fatal编程技术网

Node.js 福米克&x2B;是的,访问嵌套值时出错

Node.js 福米克&x2B;是的,访问嵌套值时出错,node.js,reactjs,formik,yup,Node.js,Reactjs,Formik,Yup,我试图同时使用Formik和Yup来验证我的用户数据,但是当我尝试访问嵌套值的错误时,例如address.line1,我会收到一个错误,说它未定义。如何使用Formik和Yup访问嵌套值的错误 请参阅Codesandbox:查看您的代码,似乎您访问的对象是错误的。您的条件是errors.line1&&toucted.address.line1,但应该是errors.address&&errors.address.line1&&toucted.address.line1 发生错误的原因是error

我试图同时使用Formik和Yup来验证我的用户数据,但是当我尝试访问嵌套值的错误时,例如
address.line1
,我会收到一个错误,说它未定义。如何使用Formik和Yup访问嵌套值的错误


请参阅Codesandbox:

查看您的代码,似乎您访问的对象是错误的。您的条件是
errors.line1&&toucted.address.line1
,但应该是
errors.address&&errors.address.line1&&toucted.address.line1

发生错误的原因是errors.address起初不存在,因为errors在开始时是空对象。您可以通过
console.log(errors)
检查这一点

我试着使用这段代码,它是有效的。()


{errors.firstName&&touch.firstName(
{errors.firstName}
):null}

//更改了条件访问和对象访问 {errors.address&&errors.address.line1&&toucted.address.line1( {errors.address.line1} ) : ( "" )}
提交
<Form>
  <Field name="firstName" placeholder="first Name" />
    {errors.firstName && touched.firstName ? (
      <div>{errors.firstName}</div>
    ) : null}
    <br />

  <Field name="address.line1" placeholder="line 1" />
    // changed the conditional and object access
    {errors.address && errors.address.line1 && touched.address.line1 ? (
      <span className="red">{errors.address.line1}</span>
    ) : (
      ""
    )}
    <br />
    <button type="submit">Submit</button>
</Form>