我不知道如何添加ID以在我的帖子和我的评论之间建立关系(React JS/strapi)

发布于 2025-01-31 14:46:19 字数 2031 浏览 7 评论 0原文

我使用React JS和Strapi。我试图向我的API发送评论。我工作了,但没有一件基本的事情:我写评论的帖子。我如何添加帖子的ID,以便在我的评论和帖子之间建立关系?

import React, { useState } from 'react'
import { useParams } from 'react-router-dom'
import TextField from '@material-ui/core/TextField';
import { Button } from '@material-ui/core'
import CommentsAPI from '../../Services/CommentsAPI'

export default function CommentForm() {

    const [comment, setComment] = useState({})
    const {id} = useParams()

    const handleSubmit = async (event) => {
        event.preventDefault();
        try {
            CommentsAPI.create(JSON.parse(`{"data":${JSON.stringify(comment)}}`))
        } catch (error) {
            console.log(error)
        }
    }

    const handleChange = (event) => {
        const {name, value} = event.currentTarget
        setComment({
            ...comment,
            [name]: value
        })
    }

    return (
        <form onSubmit={handleSubmit}>
            <div>
                <TextField 
                    id="pseudo" 
                    label="Pseudo" 
                    type="text" 
                    onChange={handleChange}
                    name="pseudo"
                />
            </div>
            <div>
                <TextField
                    id="comment"
                    label="Comment"
                    multiline
                    minRows={2}
                    onChange={handleChange}
                    name="content"
                />
            </div>
            <div>
                <Button variant="contained" color="primary" type="submit">
                    Send
                </Button>
            </div>
        </form>
    )
}
import { URL_COMMENTS } from '../config'
import axios from 'axios'

function create(id_post, comment) {
    return axios.post(URL_COMMENTS, id_post, comment)
}

const CommentsAPI = {
    create
}

export default CommentsAPI

感谢您的帮助。

I use React JS and Strapi. I am trying to send comment to my API. I worked but without one essential thing : the post for which I am writing a comment. How can I possibly add the id of my post so that the relation is made between my comment and my post ?

import React, { useState } from 'react'
import { useParams } from 'react-router-dom'
import TextField from '@material-ui/core/TextField';
import { Button } from '@material-ui/core'
import CommentsAPI from '../../Services/CommentsAPI'

export default function CommentForm() {

    const [comment, setComment] = useState({})
    const {id} = useParams()

    const handleSubmit = async (event) => {
        event.preventDefault();
        try {
            CommentsAPI.create(JSON.parse(`{"data":${JSON.stringify(comment)}}`))
        } catch (error) {
            console.log(error)
        }
    }

    const handleChange = (event) => {
        const {name, value} = event.currentTarget
        setComment({
            ...comment,
            [name]: value
        })
    }

    return (
        <form onSubmit={handleSubmit}>
            <div>
                <TextField 
                    id="pseudo" 
                    label="Pseudo" 
                    type="text" 
                    onChange={handleChange}
                    name="pseudo"
                />
            </div>
            <div>
                <TextField
                    id="comment"
                    label="Comment"
                    multiline
                    minRows={2}
                    onChange={handleChange}
                    name="content"
                />
            </div>
            <div>
                <Button variant="contained" color="primary" type="submit">
                    Send
                </Button>
            </div>
        </form>
    )
}
import { URL_COMMENTS } from '../config'
import axios from 'axios'

function create(id_post, comment) {
    return axios.post(URL_COMMENTS, id_post, comment)
}

const CommentsAPI = {
    create
}

export default CommentsAPI

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文