Express 和 Cradle 出现奇怪错误

发布于 2024-12-11 12:51:32 字数 2463 浏览 0 评论 0原文

我无法更好地解释,因为老实说我不知道​​为什么会出现这个错误。首先,这是我收到错误的代码:

app.get '/image/:id', (req, res, next) ->
    images.get req.params.id, (err, doc) ->
        if err then next err else
            res.contentType doc._attachments.image.content_type
            img = images.getAttachment doc.id, 'image'
            img.on 'data', (chunk) ->
                res.write chunk, 'binary'
            img.on 'end', ->
                res.end()

我相当确定问题一定出在某个地方。我有一个 /upload 路由重定向到此,以立即显示上传的图像。

然后我收到此错误:

TypeError: Cannot call method 'replace' of undefined
    at Object.lookup (/home/scan/Javascript/acres/node_modules/express/node_modules/mime/mime.js:62:20)
    at ServerResponse.contentType (/home/scan/Javascript/acres/node_modules/express/lib/response.js:209:43)
    at Object.callback (/home/scan/Javascript/acres/app.coffee:105:13)
    at Object.get (/home/scan/Javascript/acres/node_modules/cradle/lib/cradle.js:316:33)
    at /home/scan/Javascript/acres/app.coffee:100:19
    at callbacks (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:272:11)
    at param (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:246:11)
    at param (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:243:11)
    at pass (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:253:5)
    at Router._dispatch (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:280:4)

当然,我想查找到底是什么值给我带来了麻烦,因此我在每个嵌套中使用一些 console.log 更改了代码。 nodemon 重新启动了应用程序,对于此图像来说,它就像一个魅力。有了这个代码。

所以我上传了另一个,完全相同的错误。代码更改,重新启动,适用于此图像。

那么我哪里出错了,还是每次上传都必须重新启动?

编辑:有关系统的一些信息:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

I can't explain any better, because I honestly have no idea why this error exists. First off, here is the code where I get the error:

app.get '/image/:id', (req, res, next) ->
    images.get req.params.id, (err, doc) ->
        if err then next err else
            res.contentType doc._attachments.image.content_type
            img = images.getAttachment doc.id, 'image'
            img.on 'data', (chunk) ->
                res.write chunk, 'binary'
            img.on 'end', ->
                res.end()

I am fairly sure the problem must be in there somewhere. I have an /upload route that redirects to this, to show the uploaded image right away.

And then I get this error:

TypeError: Cannot call method 'replace' of undefined
    at Object.lookup (/home/scan/Javascript/acres/node_modules/express/node_modules/mime/mime.js:62:20)
    at ServerResponse.contentType (/home/scan/Javascript/acres/node_modules/express/lib/response.js:209:43)
    at Object.callback (/home/scan/Javascript/acres/app.coffee:105:13)
    at Object.get (/home/scan/Javascript/acres/node_modules/cradle/lib/cradle.js:316:33)
    at /home/scan/Javascript/acres/app.coffee:100:19
    at callbacks (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:272:11)
    at param (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:246:11)
    at param (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:243:11)
    at pass (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:253:5)
    at Router._dispatch (/home/scan/Javascript/acres/node_modules/express/lib/router/index.js:280:4)

Of course I wanted to look up what value exactly is giving me the trouble, so I changed the code with some console.log in every nesting. nodemon restarted the app, and it works like a charm, for this image. With this code.

So I upload another, exactly the same error. Code change, restart, works for this image.

So where do I go wrong or do I have to restart on every upload?

EDIT: Some info about the system:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

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

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

发布评论

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

评论(1

半暖夏伤 2024-12-18 12:51:32

请注意调用堆栈中的第二行:

位于 ServerResponse.contentType (/home/scan/Javascript/acres/node_modules/express/lib/response.js:209:43)

因此错误发生在您的 res.contentType 调用中,该调用传递 < code>doc._attachments.image.content_type 到 mime.lookup,它调用 .replace 。简而言之,问题在于 doc._attachments.image.content_type 未定义。

我希望这至少能让您开始调试工作。

Note the second line in the call stack:

at ServerResponse.contentType (/home/scan/Javascript/acres/node_modules/express/lib/response.js:209:43)

So the error is occurring in your res.contentType call, which passes doc._attachments.image.content_type to mime.lookup, which calls .replace on it. In short, the problem is that doc._attachments.image.content_type is undefined.

I hope that at least gets you started in your debugging efforts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文