1

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

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重新启动了应用程序,对于这张图片,它就像一个魅力。有了这个代码。

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

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

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

connect-cradle@0.1.0
connect-form@0.2.1
cradle@0.5.7
express@2.4.6
node@0.4.12
4

1 回答 1

1

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

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

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

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

于 2011-10-23T03:57:03.770 回答