如何从React前端将数组发送到MongoDB后端

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

How to send an array from a react Frontend to a mongo DB BackEnd

问题

以下是翻译好的部分:

在Postman中,我成功地通过这种方式发送数据来维护这个结构:{"categoryTags":["easy", "breakfast"]},这将创建一个看起来像这样的结构:categoryTags: 0:"easy", 1:"breakfast"。这对于后续在我的前端上进行映射非常有用。当在前端检索到信息时,它会以这种方式到达:easy, breakfast,当编辑并推送到数据库时,它看起来像这样:categoryTags:0:"easy,breakfast"。我不知道如何从前端到后端维护这个结构。这是否可能?

这是我的React代码:

  1. const EditRecipeModal = ({ recipe }) => {
  2. const oneRecipe = useSelector((state) => state.user.singleRecipe);
  3. const dispatch = useDispatch();
  4. useEffect(() => {
  5. dispatch(fetchSingleRecipe(recipe._id));
  6. }, []);
  7. const [tags, setTags] = useState([recipe.categoryTags]);
  8. const [title, setTitle] = useState(recipe.title);
  9. const [description, setDescription] = useState(recipe.description);
  10. const [ingredients, setIngredients] = useState(recipe.ingredients);
  11. const [instructions, setInstructions] = useState(recipe.instructions);
  12. const editedModal = {
  13. categoryTags: tags,
  14. title: title,
  15. description: description,
  16. ingredients: ingredients,
  17. instructions: instructions,
  18. };
  19. const onSubmitHandler = (e) => {
  20. e.preventDefault();
  21. dispatch(editRecipe(editedModal, `${recipe._id}`));
  22. };
  23. return (
  24. <div id="editRecipeModal">
  25. <h1>Edit {recipe.title}</h1>
  26. <div>
  27. <form onSubmit={onSubmitHandler}>
  28. <label className="fw-bold">Category</label>
  29. <input
  30. onChange={(e) => setTags(e.target.value)}
  31. value={tags}
  32. type="text"
  33. placeholder={recipe.categoryTags}
  34. />
  35. <Form.Group className="mb-3">
  36. <Form.Label className="fw-bold">Title</Form.Label>
  37. <Form.Control
  38. value={title}
  39. onChange={(e) => setTitle(e.target.value)}
  40. type="text"
  41. placeholder="ingredients"
  42. />
  43. </Form.Group>
  44. <Form.Group className="mb-3">
  45. <Form.Label className="fw-bold">Ingredients</Form.Label>
  46. <Form.Control
  47. value={ingredients}
  48. onChange={(e) => setIngredients(e.target.value)}
  49. type="text"
  50. placeholder="ingredients"
  51. />
  52. </Form.Group>
  53. <Form.Group className="mb-3">
  54. <Form.Label className="fw-bold">Instructions</Form.Label>
  55. <Form.Control
  56. value={instructions}
  57. onChange={(e) => setInstructions(e.target.value)}
  58. type="text"
  59. placeholder="instructions"
  60. />
  61. </Form.Group>
  62. <Button
  63. className="login-80p mb-4 fw-bold"
  64. variant="success"
  65. type="submit"
  66. >
  67. Edit
  68. </Button>
  69. </form>
  70. </div>
  71. <Button variant="danger">Delete</Button>
  72. </div>
  73. );
  74. };
  75. export default EditRecipeModal;

这是我的后端模式:

  1. const recipeSchema = new Schema({
  2. categoryTags: [{ type: String, required: true }],
  3. title: { type: String, required: true },
  4. photo: {
  5. type: String,
  6. required: false,
  7. default: "https://cdn-icons-png.flaticon.com/512/135/135161.png",
  8. },
  9. description: { type: String, required: false },
  10. ingredients: [{ type: String, required: false }],
  11. instructions: [{ type: String, required: false }],
  12. author: { type: Schema.Types.ObjectId, ref: "User", required: true },
  13. });
  14. export default model("Recipe", recipeSchema);
英文:

I have been building a recipe app (both front and back end). My issue is with the data structure and how to maintain it.

My recipe schema has a few properties that have an array of objects as values. On Postman, I am able to successfully maintain this structure by sending it like this {"categoryTags":["easy", "breakfast"]} and this creates a structure that looks like This: categoryTags: 0:"easy", 1:"breakfast"... This is great for mapping later on my frontend. The Information when retrieved on the frontend arrives like this: easy,breakfast and when edited and pushed to the db... it looks like this: categoryTags:0:"easy,breakfast". I don't know how to maintain the structure from the FE to the BE. Is this possible?

This is My React Code:

  1. const EditRecipeModal = ({ recipe }) =&gt; {
  2. const oneRecipe = useSelector((state) =&gt; state.user.singleRecipe);
  3. const dispatch = useDispatch();
  4. useEffect(() =&gt; {
  5. dispatch(fetchSingleRecipe(recipe._id));
  6. }, []);
  7. const [tags, setTags] = useState([recipe.categoryTags]);
  8. const [title, setTitle] = useState(recipe.title);
  9. const [description, setDescription] = useState(recipe.description);
  10. const [ingredients, setIngredients] = useState(recipe.ingredients);
  11. const [instructions, setInstructions] = useState(recipe.instructions);
  12. const editedModal = {
  13. categoryTags: tags,
  14. title: title,
  15. description: description,
  16. ingredients: ingredients,
  17. instructions: instructions,
  18. };
  19. const onSubmitHandler = (e) =&gt; {
  20. e.preventDefault();
  21. dispatch(editRecipe(editedModal, `${recipe._id}`));
  22. };
  23. return (
  24. &lt;div id=&quot;editRecipeModal&quot;&gt;
  25. &lt;h1&gt;Edit {recipe.title}&lt;/h1&gt;
  26. &lt;div&gt;
  27. &lt;form onSubmit={onSubmitHandler}&gt;
  28. &lt;label className=&quot;fw-bold&quot;&gt;Category&lt;/label&gt;
  29. &lt;input
  30. onChange={(e) =&gt; setTags(e.target.value)}
  31. value={tags}
  32. type=&quot;text&quot;
  33. placeholder={recipe.categoryTags}
  34. /&gt;
  35. &lt;Form.Group className=&quot;mb-3&quot;&gt;
  36. &lt;Form.Label className=&quot;fw-bold&quot;&gt;Title&lt;/Form.Label&gt;
  37. &lt;Form.Control
  38. value={title}
  39. onChange={(e) =&gt; setTitle(e.target.value)}
  40. type=&quot;text&quot;
  41. placeholder=&quot;ingredients&quot;
  42. /&gt;
  43. &lt;/Form.Group&gt;
  44. &lt;Form.Group className=&quot;mb-3&quot;&gt;
  45. &lt;Form.Label className=&quot;fw-bold&quot;&gt;Ingredients&lt;/Form.Label&gt;
  46. &lt;Form.Control
  47. value={ingredients}
  48. onChange={(e) =&gt; setIngredients(e.target.value)}
  49. type=&quot;text&quot;
  50. placeholder=&quot;ingredients&quot;
  51. /&gt;
  52. &lt;/Form.Group&gt;
  53. &lt;Form.Group className=&quot;mb-3&quot;&gt;
  54. &lt;Form.Label className=&quot;fw-bold&quot;&gt;Instructions&lt;/Form.Label&gt;
  55. &lt;Form.Control
  56. value={instructions}
  57. onChange={(e) =&gt; setInstructions(e.target.value)}
  58. type=&quot;text&quot;
  59. placeholder=&quot;instructions&quot;
  60. /&gt;
  61. &lt;/Form.Group&gt;
  62. &lt;Button
  63. className=&quot;login-80p mb-4 fw-bold&quot;
  64. variant=&quot;success&quot;
  65. type=&quot;submit&quot;
  66. &gt;
  67. Edit
  68. &lt;/Button&gt;
  69. &lt;/form&gt;
  70. &lt;/div&gt;
  71. &lt;Button variant=&quot;danger&quot;&gt;Delete&lt;/Button&gt;
  72. &lt;/div&gt;
  73. );
  74. };
  75. export default EditRecipeModal;

This is my BE Schema:

  1. const recipeSchema = new Schema({
  2. categoryTags: [{ type: String, required: true }],
  3. title: { type: String, required: true },
  4. photo: {
  5. type: String,
  6. required: false,
  7. default: &quot;https://cdn-icons-png.flaticon.com/512/135/135161.png&quot;,
  8. },
  9. description: { type: String, required: false },
  10. ingredients: [{ type: String, required: false }],
  11. instructions: [{ type: String, required: false }],
  12. author: { type: Schema.Types.ObjectId, ref: &quot;User&quot;, required: true },
  13. });
  14. export default model(&quot;Recipe&quot;, recipeSchema);

答案1

得分: 1

你可以将BE模式中的categoryTags定义为数组类型 { type: Array, required: true }

英文:

you can define the categoryTags in the BE schema as array type { type: Array, required: true }

huangapple
  • 本文由 发表于 2023年3月4日 04:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631765.html
匿名

发表评论

匿名网友

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

确定