Tijl Van den Brugghen
Tijl Van den Brugghen

Reputation: 141

error: unexpected if for unknown reasons

I don't have a clue why this happens, but here is code of the file:

express = require "express"
fs = require "fs"

router = express.Router()

module.exports = (config) ->

    fileRe = /^([a-zA-Z0-9-_]+)(\.(png|jpg|jpeg))?$/i

    router.get '/:file', (req, res) ->

        file = req.params.file

        if !file or !fileRe.test file
            return res.error(404).send "404 Not Found"

        fileInfo = fileRe.exec file

        if !fileInfo[3]
            # .png is default if extension is ommited
            # abc => abc.png, but abc.jpg will stay
            file += ".png"

        if fs.access config.destination + file, fs.F_OK, (err) ->
            if err
                res.status(500).send "An error occured"
            else
                if !fileInfo[3] or fileInfo[3] == "png"
                    ct = "image/png"
                else if fileInfo[3] == "jpg"
                    ct = "image/jpg"

                opts =
                    lastModified: false
                    headers:
                        "Content-Disposition": "inline; filename=\"#{file}\""
                        "Content-Type": ""

    return router

I get the following error

/home/kindlyfire/Webroot/uplimg-server/src/web/view.coffee:24:9: error: unexpected if
    if fs.access config.destination + file, fs.F_OK, (err) ->
    ^^

I looked at the spaces, no problem there. Has anybody an idea about what it might be ?

Upvotes: 0

Views: 705

Answers (1)

Jared Smith
Jared Smith

Reputation: 21926

What you wrote is not valid coffeescript. Specifically, it is the commas on the line the error is pointing you to. I'd offer info on how to fix it, but I can't even tell what you were trying to accomplish here. You have to provide a way for the compiler (not to mention readers) to be able to tell, unambiguously, what divisions you want in your code:

# fine
if foo then (a, b) -> c

# also fine
if foo
  (a, b) ->
    c

# ??
if foo (a, b) -> c

# ????
if foo a, b -> c

Repro of the bug. Note that this is a good example of how to make a minimum reproduction of the problem. I highly, highly recommend you read a coffeescript style guide and discipline yourself to follow it. Which one is not terribly important, its the consistency that matters. Do not just randomly copy-paste stuff from the internets into your code, re-write it to follow the same style as the rest of your code. Doing so will often have the added benefit of realizing how the snippet you copied is working.

Upvotes: 1

Related Questions