Express 和 Cradle 出现奇怪错误
我无法更好地解释,因为老实说我不知道为什么会出现这个错误。首先,这是我收到错误的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意调用堆栈中的第二行:
因此错误发生在您的
res.contentType
调用中,该调用传递 < code>doc._attachments.image.content_type 到mime.lookup
,它调用.replace
。简而言之,问题在于 doc._attachments.image.content_type 未定义。我希望这至少能让您开始调试工作。
Note the second line in the call stack:
So the error is occurring in your
res.contentType
call, which passesdoc._attachments.image.content_type
tomime.lookup
, which calls.replace
on it. In short, the problem is thatdoc._attachments.image.content_type
is undefined.I hope that at least gets you started in your debugging efforts.