HTML5视频未显示来自Axios React数据获取视频的视频

发布于 2025-01-27 03:22:37 字数 2393 浏览 2 评论 0原文

我对视频播放器页面有问题,无法找到与视频文件关联的数据。我一直找不到404或“来自我代码的Axios代码段的未定义错误,因为这是控制台日志所在的位置。但是,数据对象也出现在控制台中,但URL仍然未定义,并且视频播放器也是

后端的代码,这是通过路由: upload.js:

const { sequelize, video } = require('../models');

    router.post("/getVideo", async (req, res) => {
        
            try {
                const getvideo = await video.findOne({
                    "_id" : req.body.videoId
                })
        
                return res.json(getvideo)
            } catch (err) {
                console.log(err)
                return res.status(500).json({ error: 'Something went wrong' });
            }
        })

Sequeilize Console似乎是功能性的,

这是前端的代码: lidetvideopage.js

const DetailVideoPage = (props) => {
        let { videoId } = useParams();
        const [video, setVideo] = useState([]);
    
        const videoVariable = {
            videoId: videoId
        }
    
        useEffect(() => {
            axios.post('http://localhost:5000/api/upload/getVideo', videoVariable, {
                headers: {
                    'accept': 'application/json',
                },
            })
            .then(response => {
                if (response.data.success) {
                    console.log(response.data.video)
                    setVideo(response.data.video)
                    console.log(response.data.success);
                } else {
                    console.log(response.data);
                    alert('Failed to get video Info')
                }
        }).catch(error => {
                        console.log(error);
                    });
    
    }, []);
    
        return (
            <div className="postPage" style={{ width: '100%', padding: '3rem 4em' }} >
            <video style={{ width: '100%' }} src={`http://localhost:5000/${video.filePath}`} controls></video>
    
            </div>
        );
    }

这是我从本地主机页面收到错误的图像: localhost console

在这里是前端的路线: 从'./组件/视图/详细信息videopage';

<Router>
      <Routes>
      <Route exact path='/video/:videoId' element={<DetailVideoPage />} />
      </Routes>
    </Router>

更新:我将问题重命名以更准确地显示问题的性质。主要问题是,即使获取了数据,也没有显示视频。

I am having issue with my video player page, not being able to find the data associated with the video file. I keep getting a 404 not found or a "undefined error coming from the axios code snippet of my code as that is where the console log for it is located. However, the object of data also appears in the console but the url remains undefined and so does the video player.

Here is the code for the backend, this is through the routing:
upload.js:

const { sequelize, video } = require('../models');

    router.post("/getVideo", async (req, res) => {
        
            try {
                const getvideo = await video.findOne({
                    "_id" : req.body.videoId
                })
        
                return res.json(getvideo)
            } catch (err) {
                console.log(err)
                return res.status(500).json({ error: 'Something went wrong' });
            }
        })

The sequeilize console appears to be functional

Here is the code for the frontend:
Detailvideopage.js

const DetailVideoPage = (props) => {
        let { videoId } = useParams();
        const [video, setVideo] = useState([]);
    
        const videoVariable = {
            videoId: videoId
        }
    
        useEffect(() => {
            axios.post('http://localhost:5000/api/upload/getVideo', videoVariable, {
                headers: {
                    'accept': 'application/json',
                },
            })
            .then(response => {
                if (response.data.success) {
                    console.log(response.data.video)
                    setVideo(response.data.video)
                    console.log(response.data.success);
                } else {
                    console.log(response.data);
                    alert('Failed to get video Info')
                }
        }).catch(error => {
                        console.log(error);
                    });
    
    }, []);
    
        return (
            <div className="postPage" style={{ width: '100%', padding: '3rem 4em' }} >
            <video style={{ width: '100%' }} src={`http://localhost:5000/${video.filePath}`} controls></video>
    
            </div>
        );
    }

Here is an image of the error I receive from my localhost page: localhost console

Here is the routes for the frontend:
import DetailVideoPage from './components/views/DetailVideoPage/DetailVideoPage';

<Router>
      <Routes>
      <Route exact path='/video/:videoId' element={<DetailVideoPage />} />
      </Routes>
    </Router>

UPDATE: I have renamed the question to more accurately display the nature of the issue. The main issue is that even though the data is fetched, the video is not being displayed.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

游魂 2025-02-03 03:22:37

解决方案:
我已经在VC的建议和一些研究的建议下找到了解决方案,这要归功于他!

我已经为我的详细信息重构了一些代码:

function DetailVideoPage() {
  const [Video, setVideo] = useState([]);

  const videoId = useParams();

  const videoVariable = {
    videoId: videoId
  }

  useEffect(() => {

    const getSingleVideoData = async () => {
      axios.post('http://localhost:5000/api/upload/getVideo', videoVariable)
      .then(response => {
              console.log(response);
              setVideo(response.data.filePath);
            //   let myPath = response.data.filePath; 
            //   alert( "MP4 path is : " + myPath);
      })
      .catch(function (error) {
          console.log(error);
      });

    }
    getSingleVideoData();

  }, []);


  return (

    <>
      <Row>
          <Col lg={18} xs={24}>
          <div className="postPage" style={{ width: '100%', padding: '3rem 4em' }}>
          <video style={{ width: '100%' }} src={`http://localhost:5000/${Video}`} type='video/mp4' controls autoPlay muted />
          </div>
          </Col>
      </Row>

      </>
  )

}

SOLUTION:
I have found the solution with the advice of VC.One and a bit of research, much thanks to him!

I have refactored some of my code for my DetailVideoPage.JS:

function DetailVideoPage() {
  const [Video, setVideo] = useState([]);

  const videoId = useParams();

  const videoVariable = {
    videoId: videoId
  }

  useEffect(() => {

    const getSingleVideoData = async () => {
      axios.post('http://localhost:5000/api/upload/getVideo', videoVariable)
      .then(response => {
              console.log(response);
              setVideo(response.data.filePath);
            //   let myPath = response.data.filePath; 
            //   alert( "MP4 path is : " + myPath);
      })
      .catch(function (error) {
          console.log(error);
      });

    }
    getSingleVideoData();

  }, []);


  return (

    <>
      <Row>
          <Col lg={18} xs={24}>
          <div className="postPage" style={{ width: '100%', padding: '3rem 4em' }}>
          <video style={{ width: '100%' }} src={`http://localhost:5000/${Video}`} type='video/mp4' controls autoPlay muted />
          </div>
          </Col>
      </Row>

      </>
  )

}

↙厌世 2025-02-03 03:22:37

用于测试...查看是否在页面上显示视频:

const DetailVideoPage = (props) => {
        let { videoId } = useParams();
        const [video, setVideo] = useState([]);
    
        const videoVariable = { videoId: videoId }
        
        var myDiv; var myVid;
    
        useEffect(() => {
            axios.post('http://localhost:5000/api/upload/getVideo', videoVariable, {
                headers: {
                    'accept': 'application/json',
                },
            })
            .then(response => {
                if (response.data.success) 
                {
                    console.log(response.data.video)
                    setVideo(response.data.video)
                    console.log(response.data.success);
                    
                    ///////////////////////////////////
                    //# test adding of Video
                    
                    //# create the Div
                    myDiv = document.createElement('div');
                    myDiv.setAttribute("id", "myDiv");

                    //# create the Video object
                    myVid = document.createElement( "video");
                    myVid.setAttribute("id", "myVid");
                    myVid.setAttribute("controls", "true" );
                    myVid.setAttribute("height", "500");
                    myVid.setAttribute("src", path);
                    myVid.load();

                    //# add Video into Div, then finally add Div to page
                    myDiv.appendChild( myVid );
                    document.body.appendChild( myDiv );
                    
                    ///////////////////////////////////
                } 
                else 
                {
                    console.log(response.data);
                    alert('Failed to get video Info')
                }
        }).catch(error => {
                        console.log(error);
                    });
    
    }, []);
        
        //# If needed... see if returning the Div works...
        //return ( myDiv );
        
        /*
        return (
            <div className="postPage" style={{ width: '100%', padding: '3rem 4em' }} >
            <video style={{ width: '100%' }} src={`http://localhost:5000/${video.filePath}`} controls></video>
    
            </div>
        );
        */
}

For testing... See if this shows a video on the page:

const DetailVideoPage = (props) => {
        let { videoId } = useParams();
        const [video, setVideo] = useState([]);
    
        const videoVariable = { videoId: videoId }
        
        var myDiv; var myVid;
    
        useEffect(() => {
            axios.post('http://localhost:5000/api/upload/getVideo', videoVariable, {
                headers: {
                    'accept': 'application/json',
                },
            })
            .then(response => {
                if (response.data.success) 
                {
                    console.log(response.data.video)
                    setVideo(response.data.video)
                    console.log(response.data.success);
                    
                    ///////////////////////////////////
                    //# test adding of Video
                    
                    //# create the Div
                    myDiv = document.createElement('div');
                    myDiv.setAttribute("id", "myDiv");

                    //# create the Video object
                    myVid = document.createElement( "video");
                    myVid.setAttribute("id", "myVid");
                    myVid.setAttribute("controls", "true" );
                    myVid.setAttribute("height", "500");
                    myVid.setAttribute("src", path);
                    myVid.load();

                    //# add Video into Div, then finally add Div to page
                    myDiv.appendChild( myVid );
                    document.body.appendChild( myDiv );
                    
                    ///////////////////////////////////
                } 
                else 
                {
                    console.log(response.data);
                    alert('Failed to get video Info')
                }
        }).catch(error => {
                        console.log(error);
                    });
    
    }, []);
        
        //# If needed... see if returning the Div works...
        //return ( myDiv );
        
        /*
        return (
            <div className="postPage" style={{ width: '100%', padding: '3rem 4em' }} >
            <video style={{ width: '100%' }} src={`http://localhost:5000/${video.filePath}`} controls></video>
    
            </div>
        );
        */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文