Question

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:

connect-cradle@0.1.0
connect-form@0.2.1
cradle@0.5.7
express@2.4.6
node@0.4.12
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top