I’m doing a form filling project that stores data from react to nodejs and then to database. I am unable to pass the data from react to node

huangapple go评论94阅读模式
英文:

I’m doing a form filling project that stores data from react to nodejs and then to database. I am unable to pass the data from react to node

问题

这是您提供的代码段,用于将数据从React传递到Node.js,然后存储到数据库中。请问您需要关于这段代码的帮助或有特定的问题吗?

英文:

I’m doing a form filling project that stores data from react to nodejs and then to database. I am unable to pass the data from react to node when using FORMIK I need to know what extra should I code to solve the issue
my code for your reference :

Please find the following code with the issue.

  1. import * as yup from "yup";
  2. import { useFormik } from "formik";
  3. const formValidation = yup.object({
  4. NameoftheUPSIProject: yup
  5. .string()
  6. .required("Fill the Name of the UPSI Project"),
  7. InformationSharedBy: yup.string().required("Fill the Information Shared By"),
  8. InfoSharedwithrespectto: yup.string().required("A radio option is required"),
  9. InsiderTypes: yup.string().required("Fill the Insider Types"),
  10. ContactPerson: yup.string().required("Fill the Contact Person"),
  11. NameoftheOrganization: yup
  12. .string()
  13. .required("Fill the Name of the Organization"),
  14. DateofSharing: yup.string().required("Fill the Date and Time of Sharing"),
  15. ParticularofInformationShared: yup
  16. .string()
  17. .required("Fill the Particular of Information Shared"),
  18. PurposeofSharing: yup.string().required("Fill the Purpose of Sharing"),
  19. ModeofSharing: yup.string().required("Fill the Mode of Sharing"),
  20. });
  21. const apple = (data) => {
  22. console.log(data);
  23. fetch(`localhost:5000/details`, {
  24. method: "POST",
  25. body: JSON.stringify(data),
  26. headers: { "Content-Type": "application/json" },
  27. });
  28. };
  29. const DetailsList = () => {
  30. const formik = useFormik({
  31. initialValues: {
  32. NameoftheUPSIProject: "",
  33. InformationSharedBy: "",
  34. InfoSharedwithrespectto: "",
  35. InsiderTypes: "",
  36. ContactPerson: "",
  37. NameoftheOrganization: "",
  38. DateofSharing: "",
  39. ParticularofInformationShared: "",
  40. PurposeofSharing: "",
  41. ModeofSharing: "",
  42. },
  43. validationSchema: formValidation,
  44. onSubmit: async (values) => {
  45. console.log(values);
  46. try {
  47. setloading(true);
  48. await axios.post(`http:localhost:5000/api/detailsnewform`, values);
  49. setloading(false);
  50. alert("Form Created Successfully");
  51. formik.resetForm();
  52. } catch (error) {
  53. console.log(error);
  54. }
  55. },
  56. });
  57. return (
  58. <form onSubmit={formik.handleSubmit}>
  59. <div>
  60. <div>
  61. {/* <div class="mb-3 row">
  62. <label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
  63. <div class="col-sm-10">
  64. <input type="text" class="form-control-plaintext" id="staticEmail" value="email@example.com"/>
  65. </div> */}
  66. <div class="mb-3 row">
  67. <label for="text" class="col-sm-2 col-form-label">
  68. Name of the UPSI Project:
  69. </label>
  70. <div class="col-sm-10">
  71. <input type="text" class="form-control" id="inputPassword" />
  72. </div>
  73. </div>
  74. <div class="mb-3 row">
  75. <label for="inputPassword" class="col-sm-2 col-form-label">
  76. Information Shared By:
  77. </label>
  78. <div class="col-sm-10">
  79. <input type="text" class="form-control" id="inputPassword" />
  80. </div>
  81. </div>
  82. <div class="mb-3 row">
  83. <label for="text" class="col-sm-2 col-form-label">
  84. Info Shared with respect to:
  85. </label>
  86. <div class="col-sm-10">
  87. <input type="radio" name="reason" value="Fit Description" />
  88. Designated Person
  89. <div>
  90. <input type="radio" name="reason" value="Fit Description" />
  91. Insider
  92. </div>
  93. <div>
  94. <input type="radio" name="reason" value="Fit Description" />
  95. UPSI Project
  96. </div>
  97. </div>
  98. </div>
  99. <div class="mb-3 row">
  100. <label for="text" class="col-sm-2 col-form-label">
  101. Insider Types:
  102. </label>
  103. <div class="col-sm-10">
  104. <select
  105. class="form-select form-select-lg mb-3"
  106. aria-label=".form-select-lg example"
  107. >
  108. <option selected>Director</option>
  109. <option value="1">Managing Director</option>
  110. <option value="2">Independent Director</option>
  111. <option value="3">CFO</option>
  112. <option value="4">Company Secretary</option>
  113. </select>
  114. </div>
  115. </div>
  116. </div>
  117. <div class="mb-3 row">
  118. <label for="text" class="col-sm-2 col-form-label">
  119. Insider Signed Confidentially Agreement:
  120. </label>
  121. <div class="col-sm-10">
  122. <label
  123. for="text"
  124. class="col-sm-2 col-form-label"
  125. style={{ color: "red" }}
  126. >
  127. No Confidentially Agreement Signed
  128. </label>
  129. </div>
  130. </div>
  131. <div class="mb-3 row">
  132. <label for="text" class="col-sm-2 col-form-label">
  133. Contact Person:
  134. </label>
  135. <div class="col-sm-10">
  136. <input type="text" class="form-control" id="inputPassword" />
  137. </div>
  138. <div class="mb-3 row">
  139. <label for="text" class="col-sm-2 col-form-label">
  140. Type of Organization:
  141. </label>
  142. <div class="col-sm-10">
  143. <label for="text" class="col-sm-2 col-form-label">
  144. Partnership
  145. </label>
  146. </div>
  147. </div>
  148. <div class="mb-3 row">
  149. <label for="text" class="col-sm-2 col-form-label">
  150. Name of the Organization:
  151. </label>
  152. <div class="col-sm-10">
  153. <input type="text" class="form-control" id="inputPassword" />
  154. </div>
  155. </div>
  156. <div class="mb-3 row">
  157. <label for="text" class="col-sm-2 col-form-label">
  158. Date and Time of Sharing:
  159. </label>
  160. <div class="col-sm-10">
  161. <input type="date" id="date" name="trip-start" />
  162. <script src="date.js"></script>
  163. </div>
  164. </div>
  165. <div class="mb-3 row">
  166. <label for="text" class="col-sm-2 col-form-label">
  167. Particular of Information Shared:
  168. </label>
  169. <div class="col-sm-10">
  170. <input type="text" class="form-control" id="inputPassword" />
  171. </div>
  172. </div>
  173. <div class="mb-3 row">
  174. <label for="text" class="col-sm-2 col-form-label">
  175. Purpose of Sharing:
  176. </label>
  177. <div class="col-sm-10">
  178. <input type="text" class="form-control" id="inputPassword" />
  179. </div>
  180. <div class="mb-3 row">
  181. <label for="text" class="col-sm-2 col-form-label">
  182. Mode of Sharing:
  183. </label>
  184. <div class="col-sm-10">
  185. <select
  186. class="form-select form-select-lg mb-3"
  187. aria-label=".form-select-lg example"
  188. >
  189. <option selected>Email</option>
  190. <option value="1">Physical</option>
  191. <option value="2">Pendrive</option>
  192. </select>
  193. <div class="mb-3 row">
  194. {/* <input type="submit" value="Submit"/> */}
  195. <button type="submit" className="button">
  196. SUBMIT
  197. </button>
  198. </div>
  199. </div>
  200. </div>
  201. </div>
  202. </div>
  203. </div>
  204. <script></script>
  205. </form>
  206. );
  207. };
  208. export default DetailsList;

I tried using above code but unable to pass the data

答案1

得分: 1

Here is the translated code part:

  1. 这是使用formik的示例
  2. import React from 'react';
  3. import { Formik, Form, Field, ErrorMessage } from 'formik';
  4. import * as Yup from 'yup';
  5. import { Button, Grid, Typography, FormControl, InputLabel, Input, FormHelperText } from '@mui/material';
  6. const SignupForm = () => {
  7. return (
  8. <div>
  9. <Typography variant='h4' align='center' gutterBottom>
  10. 注册
  11. </Typography>
  12. <Formik
  13. initialValues={{
  14. firstName: '',
  15. lastName: '',
  16. email: '',
  17. password: '',
  18. confirmPassword: '',
  19. address: '',
  20. phone: '',
  21. }}
  22. validationSchema={Yup.object({
  23. firstName: Yup.string().required('名字不能为空'),
  24. lastName: Yup.string().required('姓氏不能为空'),
  25. email: Yup.string().email('无效的电子邮件地址').required('电子邮件不能为空'),
  26. password: Yup.string()
  27. .required('密码不能为空')
  28. .min(8, '密码太短,应至少为8个字符')
  29. .matches(
  30. /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,
  31. '密码必须包含至少一个大写字母、一个小写字母、一个数字和一个特殊字符',
  32. ),
  33. confirmPassword: Yup.string()
  34. .oneOf([Yup.ref('password'), null], '密码必须匹配')
  35. .required('确认密码不能为空'),
  36. address: Yup.string().required('地址不能为空'),
  37. phone: Yup.string()
  38. .matches(/^\+?\d{10,14}$/, '无效的电话号码')
  39. .required('电话号码不能为空'),
  40. })}
  41. onSubmit={(values, { setSubmitting }) => {
  42. setTimeout(() => {
  43. alert(JSON.stringify(values, null, 2));
  44. setSubmitting(false);
  45. }, 400);
  46. }}
  47. >
  48. {({ errors, touched, isSubmitting }) => (
  49. <Form>
  50. <Grid container spacing={2}>
  51. <Grid item xs={12} sm={6}>
  52. <FormControl fullWidth error={touched.firstName && !!errors.firstName}>
  53. <InputLabel htmlFor='firstName'>名字</InputLabel>
  54. <Field as={Input} id='firstName' name='firstName' variant='outlined' />
  55. <FormHelperText>
  56. <ErrorMessage name='firstName' />
  57. </FormHelperText>
  58. </FormControl>
  59. </Grid>
  60. <Grid item xs={12} sm={6}>
  61. <FormControl fullWidth error={touched.lastName && !!errors.lastName}>
  62. <InputLabel htmlFor='lastName'>姓氏</InputLabel>
  63. <Field as={Input} id='lastName' name='lastName' variant='outlined' />
  64. <FormHelperText>
  65. <ErrorMessage name='lastName' />
  66. </FormHelperText>
  67. </FormControl>
  68. </Grid>
  69. <Grid item xs={12}>
  70. <FormControl fullWidth error={touched.email && !!errors.email}>
  71. <InputLabel htmlFor='email'>电子邮件</InputLabel>
  72. <Field as={Input} id='email' name='email' type='email' variant='outlined' />
  73. <FormHelperText>
  74. <ErrorMessage name='email' />
  75. </FormHelperText>
  76. </FormControl>
  77. </Grid>
  78. <Grid item xs={12}>
  79. <FormControl fullWidth error={touched.password && !!errors.password}>
  80. <InputLabel htmlFor='password'>密码</InputLabel>
  81. <Field as={Input} id='password' name='password' type='password' variant='outlined' />
  82. <FormHelperText>
  83. <ErrorMessage name='password' />
  84. </FormHelperText>
  85. </FormControl>
  86. </Grid>
  87. <Grid item xs={12}>
  88. <FormControl fullWidth error={touched.confirmPassword && !!errors.confirmPassword}>
  89. <InputLabel htmlFor='confirmPassword'>确认密码</InputLabel>
  90. <Field as={Input} id='confirmPassword' name='confirmPassword' type='password' variant='outlined' />
  91. <FormHelperText>
  92. <ErrorMessage name='confirmPassword' />
  93. </FormHelperText>
  94. </FormControl>
  95. </Grid>
  96. <Grid item xs={12}>
  97. <FormControl fullWidth error={touched.address && !!errors.address}>
  98. <InputLabel htmlFor='address'>地址</InputLabel>
  99. <Field as={Input} id='address' name='address' variant='outlined' />
  100. <FormHelperText>
  101. <ErrorMessage name='address' />
  102. </FormHelperText>
  103. </FormControl>
  104. </Grid>
  105. <Grid item xs={12}>
  106. <FormControl fullWidth error={touched.phone && !!errors.phone}>
  107. <InputLabel htmlFor='phone'>电话号码</InputLabel>
  108. <Field as={Input} id='phone' name='phone' variant='outlined' />
  109. <FormHelperText>
  110. <ErrorMessage name='phone' />
  111. </FormHelperText>
  112. </FormControl>
  113. </Grid>
  114. <Grid item xs={12}>
  115. <Button variant='contained' color='primary' type='submit' disabled={isSubmitting}>
  116. 提交
  117. </Button>
  118. </Grid>
  119. </Grid>
  120. </Form>
  121. )}
  122. </Formik>
  123. </div>
  124. );
  125. };
  126. export default SignupForm;

Please note that I've translated the comments and error messages in the code.

英文:

This is an example of using formik

  1. import React from &#39;react&#39;;
  2. import { Formik, Form, Field, ErrorMessage } from &#39;formik&#39;;
  3. import * as Yup from &#39;yup&#39;;
  4. import { Button, Grid, Typography, FormControl, InputLabel, Input, FormHelperText } from &#39;@mui/material&#39;;
  5. const SignupForm = () =&gt; {
  6. return (
  7. &lt;div&gt;
  8. &lt;Typography variant=&#39;h4&#39; align=&#39;center&#39; gutterBottom&gt;
  9. Signup
  10. &lt;/Typography&gt;
  11. &lt;Formik
  12. initialValues={{
  13. firstName: &#39;&#39;,
  14. lastName: &#39;&#39;,
  15. email: &#39;&#39;,
  16. password: &#39;&#39;,
  17. confirmPassword: &#39;&#39;,
  18. address: &#39;&#39;,
  19. phone: &#39;&#39;,
  20. }}
  21. validationSchema={Yup.object({
  22. firstName: Yup.string().required(&#39;First name is required&#39;),
  23. lastName: Yup.string().required(&#39;Last name is required&#39;),
  24. email: Yup.string().email(&#39;Invalid email address&#39;).required(&#39;Email is required&#39;),
  25. password: Yup.string()
  26. .required(&#39;Password is required&#39;)
  27. .min(8, &#39;Password is too short - should be 8 characters or longer&#39;)
  28. .matches(
  29. /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&amp;])[A-Za-z\d@$!%*?&amp;]{8,}$/,
  30. &#39;Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character&#39;,
  31. ),
  32. confirmPassword: Yup.string()
  33. .oneOf([Yup.ref(&#39;password&#39;), null], &#39;Passwords must match&#39;)
  34. .required(&#39;Confirm password is required&#39;),
  35. address: Yup.string().required(&#39;Address is required&#39;),
  36. phone: Yup.string()
  37. .matches(/^\+?\d{10,14}$/, &#39;Invalid phone number&#39;)
  38. .required(&#39;Phone number is required&#39;),
  39. })}
  40. onSubmit={(values, { setSubmitting }) =&gt; {
  41. setTimeout(() =&gt; {
  42. alert(JSON.stringify(values, null, 2));
  43. setSubmitting(false);
  44. }, 400);
  45. }}
  46. &gt;
  47. {({ errors, touched, isSubmitting }) =&gt; (
  48. &lt;Form&gt;
  49. &lt;Grid container spacing={2}&gt;
  50. &lt;Grid item xs={12} sm={6}&gt;
  51. &lt;FormControl fullWidth error={touched.firstName &amp;&amp; !!errors.firstName}&gt;
  52. &lt;InputLabel htmlFor=&#39;firstName&#39;&gt;First Name&lt;/InputLabel&gt;
  53. &lt;Field as={Input} id=&#39;firstName&#39; name=&#39;firstName&#39; variant=&#39;outlined&#39; /&gt;
  54. &lt;FormHelperText&gt;
  55. &lt;ErrorMessage name=&#39;firstName&#39; /&gt;
  56. &lt;/FormHelperText&gt;
  57. &lt;/FormControl&gt;
  58. &lt;/Grid&gt;
  59. &lt;Grid item xs={12} sm={6}&gt;
  60. &lt;FormControl fullWidth error={touched.lastName &amp;&amp; !!errors.lastName}&gt;
  61. &lt;InputLabel htmlFor=&#39;lastName&#39;&gt;Last Name&lt;/InputLabel&gt;
  62. &lt;Field as={Input} id=&#39;lastName&#39; name=&#39;lastName&#39; variant=&#39;outlined&#39; /&gt;
  63. &lt;FormHelperText&gt;
  64. &lt;ErrorMessage name=&#39;lastName&#39; /&gt;{&#39; &#39;}
  65. &lt;/FormHelperText&gt;
  66. &lt;/FormControl&gt;
  67. &lt;/Grid&gt;
  68. &lt;Grid item xs={12}&gt;
  69. &lt;FormControl fullWidth error={touched.email &amp;&amp; !!errors.email}&gt;
  70. &lt;InputLabel htmlFor=&#39;email&#39;&gt;Email&lt;/InputLabel&gt;
  71. &lt;Field as={Input} id=&#39;email&#39; name=&#39;email&#39; type=&#39;email&#39; variant=&#39;outlined&#39; /&gt;
  72. &lt;FormHelperText&gt;
  73. &lt;ErrorMessage name=&#39;email&#39; /&gt;
  74. &lt;/FormHelperText&gt;
  75. &lt;/FormControl&gt;
  76. &lt;/Grid&gt;
  77. &lt;Grid item xs={12}&gt;
  78. &lt;FormControl fullWidth error={touched.password &amp;&amp; !!errors.password}&gt;
  79. &lt;InputLabel htmlFor=&#39;password&#39;&gt;Password&lt;/InputLabel&gt;
  80. &lt;Field as={Input} id=&#39;password&#39; name=&#39;password&#39; type=&#39;password&#39; variant=&#39;outlined&#39; /&gt;
  81. &lt;FormHelperText&gt;
  82. &lt;ErrorMessage name=&#39;password&#39; /&gt;
  83. &lt;/FormHelperText&gt;
  84. &lt;/FormControl&gt;
  85. &lt;/Grid&gt;
  86. &lt;Grid item xs={12}&gt;
  87. &lt;FormControl fullWidth error={touched.confirmPassword &amp;&amp; !!errors.confirmPassword}&gt;
  88. &lt;InputLabel htmlFor=&#39;confirmPassword&#39;&gt;Confirm Password&lt;/InputLabel&gt;
  89. &lt;Field as={Input} id=&#39;confirmPassword&#39; name=&#39;confirmPassword&#39; type=&#39;password&#39; variant=&#39;outlined&#39; /&gt;
  90. &lt;FormHelperText&gt;
  91. &lt;ErrorMessage name=&#39;confirmPassword&#39; /&gt;
  92. &lt;/FormHelperText&gt;
  93. &lt;/FormControl&gt;
  94. &lt;/Grid&gt;
  95. &lt;Grid item xs={12}&gt;
  96. &lt;FormControl fullWidth error={touched.address &amp;&amp; !!errors.address}&gt;
  97. &lt;InputLabel htmlFor=&#39;address&#39;&gt;Address&lt;/InputLabel&gt;
  98. &lt;Field as={Input} id=&#39;address&#39; name=&#39;address&#39; variant=&#39;outlined&#39; /&gt;
  99. &lt;FormHelperText&gt;
  100. &lt;ErrorMessage name=&#39;address&#39; /&gt;
  101. &lt;/FormHelperText&gt;
  102. &lt;/FormControl&gt;
  103. &lt;/Grid&gt;
  104. &lt;Grid item xs={12}&gt;
  105. &lt;FormControl fullWidth error={touched.phone &amp;&amp; !!errors.phone}&gt;
  106. &lt;InputLabel htmlFor=&#39;phone&#39;&gt;Phone&lt;/InputLabel&gt;
  107. &lt;Field as={Input} id=&#39;phone&#39; name=&#39;phone&#39; variant=&#39;outlined&#39; /&gt;
  108. &lt;FormHelperText&gt;
  109. &lt;ErrorMessage name=&#39;phone&#39; /&gt;
  110. &lt;/FormHelperText&gt;
  111. &lt;/FormControl&gt;
  112. &lt;/Grid&gt;
  113. &lt;Grid item xs={12}&gt;
  114. &lt;Button variant=&#39;contained&#39; color=&#39;primary&#39; type=&#39;submit&#39; disabled={isSubmitting}&gt;
  115. Submit
  116. &lt;/Button&gt;
  117. &lt;/Grid&gt;
  118. &lt;/Grid&gt;
  119. &lt;/Form&gt;
  120. )}
  121. &lt;/Formik&gt;
  122. &lt;/div&gt;
  123. );
  124. };
  125. export default SignupForm;

Then inside the onSubmit function you can call the backend.

here you can find the repo https://github.com/radika-insfra/formik-sample

I've checked your code the axios call should be like this,

  1. axios
  2. .post(`http:localhost:5000/api/detailsnewform`, values)
  3. .then((response) =&gt; {
  4. console.log(response.data);
  5. })
  6. .catch((e) =&gt; {
  7. console.log(e)
  8. });

if you need help in nodejs side, please have a comment.

huangapple
  • 本文由 发表于 2023年5月11日 14:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定