0

I am building a function to read from a file input using jQuery. I have gotten the function working, however I am receiving a syntax warning in the console. Here is my code:

function (el) {

    function () {
        var file = el.files[0];
        if (file) {
            read = new FileReader();
            read.readAsDataURL(file);
        }

        return read.result;
    }
}

It appears the error is occurring due to the code on line 3 function () {, and the error reads:

Uncaught SyntaxError: Unexpected token (

Any ideas?

1
  • 1
    what is the purpose of setting an anonymous function which is anyway not called? Commented Dec 17, 2017 at 14:39

2 Answers 2

2

An anonymous function must be part of an expression, e.g. an assignment, immediatly called, or passed as argument.

But you do not use the anonymous in any way, and this is a syntax error.

At the given place only a named function would be valid, so the parser expects a function identifier but finds a ( and as of that it throws:

Uncaught SyntaxError: Unexpected token (

Sign up to request clarification or add additional context in comments.

4 Comments

For some reason when I name the function, the code no longer works.
@easports611 what do you mean with no longer works does your code work when the console shows the Uncaught SyntaxError: Unexpected token ( error?
Yes, it works with the syntax warning, but dies not work when I name the function (but the warning goes away).
@easports611 The complete file in which the SyntaxError occurs will not be executed. Therefore, the code responsible for functioning is not in that file. As soon as you fix the SyntaxError the whole code in that file is executed. So you need to check what in the file might break your code. But that's out of the scope of this question.
0

You need to name your functions like t.niese comment: An anonymous function must be part of an expression, e.g. an assignment, immediatly called, or passed as argument. But you do not use the anonymous in any way, and this is a syntax error.

function read(el) {

    function readFile() {
        var file = el.files[0];
        if (file) {
            read = new FileReader();
            read.readAsDataURL(file);
        }

        return read.result;
    }
}

1 Comment

I actually tried naming the function, and this breaks the functionality of the code (no visible errors, but data does not get sent to server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.