Getting a strange unexpected token with creating a file upload in React.js

- 1 answer

Ad

I am using React as framework to create input for a project. Input is a name, photo and an optional comment. The name and comment I got working, but the photo (being a file) gives me some trouble.

I am using this as a method for uploading. (I know that the quality of the photo must be very low to use base64 for it, but that is not a problem for this project.)

However, when I used this code, my browser threw an Unexpected token error in the console, at a location where there was just a tab. I have located the error to this piece of code (by commenting out different pieces).

reader.onload = function(output){
      fileUpload.set({
         file: output.target.result
      });
      $.when(fileUpload.save())
        .done(function(){
        this.setState({
          uploaded: true
        });
      }.bind(this));
    }.bind(this));

I just can't see what is wrong with it and why it is throwing that error.

Ad

Answer

Ad

It looks like your parens may be mismatched. Here's the same code with additional formatting:

reader.onload = function(output) {

  fileUpload.set({ file: output.target.result });

  $.when(fileUpload.save())
  .done(
    function() {
      this.setState({ uploaded: true });
    }.bind(this)
  );

}.bind(this)
); // extra

The last line has an additional ) that does not have a matching ( at the start of the expression.

Ad
source: stackoverflow.com
Ad