微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我不断收到“传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串”错误

如何解决我不断收到“传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串”错误

我对路由参数有一个奇怪的问题。我正在尝试制作一个简单的视频流系统,我将视频的 id(使用路由参数)传递到管道流中,然后将实际视频流回。

视频确实返回,但我收到“传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串”错误,这会破坏整个 html 页面

如果有人能帮助我找到解决这个问题的正确方向,我将不胜感激!

视频获取路径

app.get('/v/:id',(req,res)=> {
    const videoId = mongoose.mongo.ObjectID(req.params.id)

    gfs.files.findOne({_id: videoId},(err,file)=> {
        if(err) {
            throw err
        } else if(!file) {
            res.redirect('/')
        } else if(file) {
            res.render('Video.ejs',{file: file,title: "Video :: Clipit"})
        }
    })
})

视频管道流路由

app.get('/api/stream/:filename',res)=> {
    gfs.files.findOne({filename: req.params.filename},file)=> {
        if(err) {
            throw err
        } else {
            const readstream = gfs.createReadStream(file.filename)
            readstream.pipe(res)
        }
    })
})

视频 HTML 文件 (EJS)

<!DOCTYPE html>
    <html lang="en">
    <head>
        <Meta charset="UTF-8">
        <Meta http-equiv="X-UA-Compatible" content="IE=edge">
        <Meta name="viewport" content="width=device-width,initial-scale=1.0">
        <%- include('./partials/Settings.ejs') %>
        <title><%= title %></title>
    </head>
    <body>
        <header>
            <%- include('./partials/Header.ejs') %>
        </header>
        <main>
            <div>
                <video id="videoPlayer" width="650" controls autoplay>
                    <source src="/api/stream/<%= file.filename %>" type="<%= file.contentType %>"/>
                </video>
            </div>
        </main>
    </body>
</html>

错误

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (C:\Users\gabri\Desktop\clipit\node_modules\bson\lib\bson\objectid.js:59:11)
    at Function.ObjectID (C:\Users\gabri\Desktop\clipit\node_modules\bson\lib\bson\objectid.js:40:43)
    at C:\Users\gabri\Desktop\clipit\server.js:66:36
    at Layer.handle [as handle_request] (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:281:22
    at param (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:354:14)
    at param (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:410:3)        
    at next (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:275:10)
    at urlencodedParser (C:\Users\gabri\Desktop\clipit\node_modules\body-parser\lib\types\urlencoded.js:91:7)
    at Layer.handle [as handle_request] (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:284:7
(node:7784) DeprecationWarning: GridStore is deprecated,and will be removed in a future version. Please use GridFSBucket instead

注意:我使用 GridFS Storage 来存储视频,使用 GridFS Stream 来流式传输视频。 提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。