javascriptreactjsmeteoramazon-s3meteor-slingshot

edgee:slingshot error [Invalid directive]


So I have been trying to add edgee:slingshot to my Meteor + React project but for some reason I keep getting the same error: "Error: The directive myFileUploads does not seem to exist [Invalid directive]"

I have followed the tutorial to implement all the information but can't seem to figure out what's wrong with my code.

Any help is highly appreciated.

My upload-rules file:

Slingshot.fileRestrictions("myFileUploads", {
  allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
  maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});

Slingshot.createDirective("myFileUploads", Slingshot.S3Storage, {
  bucket:                "ec2016", 
  region:                 "eu-central-1",
  acl:                    "public-read",

  authorize: function () {
    //Deny uploads if user is not logged in.
    if (!this.userId) {
      var message = "Please login before posting files";
      throw new Meteor.Error("Login Required", message);
    }

    return true;
  },

  key: function (file) {
    //Store file into a directory by the user's username.
    var user = Meteor.users.findOne(this.userId);
    return user.username + "/" + file.name;
  }
});

My form:

export default class AddSpark extends Component {
  constructor(props) {
    super(props);

    this.upload = this.upload.bind(this)
  }

  createSpark(event){
    event.preventDefault();
    const spark = {
      city: this.city.value,
      person: this.person.value,
      location: this.location.value,
      title: this.title.value,
      content: this.content.value,
      url: this.url.value,
    }
    console.log(spark);
  }

  componentWillMount(){
    // we create this rule both on client and server
    Slingshot.fileRestrictions("myFileUploads", {
      allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
      maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
    });
  }


  upload(){
    var uploader = new Slingshot.Upload("myFileUploads");

    uploader.send(document.getElementById('input').files[0], function (error, downloadUrl) {
      if (error) {
        // Log service detailed response
        alert (error);
      }
      else {
        Meteor.users.update(Meteor.userId(), {$push: {"profile.files": downloadUrl}});
      }
    });
  }

  render() {
    return (
      <div>
        <form  ref={(input) => this.sparkForm = input} onSubmit={(e) => this.createSpark(e)}>

            <ControlLabel>Select your city</ControlLabel>
            <select id="formControlsCity" placeholder="Choose your city" className="form-control" onClick={ moreOptions } ref={(input) => this.city = input}>
              <option value="select">Choose your city</option>
              <option value="Beijing">Beijing</option>
              <option value="Shanghai">Shanghai</option>
              <option value="Chengdu & Chongqing">Chengdu & Chongqing</option>
            </select>
       
            <ControlLabel>Select your person</ControlLabel>
            <select id="formControlsPerson" placeholder="Choose your person" className="form-control" ref={(input) => this.person = input}>
              <option value="select">First select your city</option>
            </select>
    

            <ControlLabel>Select your location</ControlLabel>
            <select id="formControlsLocation" placeholder="Choose your location" className="form-control" ref={(input) => this.location = input}>
              <option value="select">First select your city</option>
            </select>

            <ControlLabel>Title</ControlLabel>
            <input type="text" label="Title" placeholder="Enter your title" className="form-control" ref={(input) => this.title = input}/>
          

            <ControlLabel>Content</ControlLabel>
            <textarea placeholder="Enter your comment here" className="form-control" ref={(input) => this.content = input}/>

          <div className="upload-area">
              <p className="alert alert-success text-center">
                <span>Click or Drag an Image Here to Upload</span>
                <input type="file" id="input" ref={(input) => this.url = input} onChange={this.upload} />
              </p>
          </div>

          <Button type="submit">Submit</Button>
        </form>
      </div>
  )}
}

Beside that I ofcourse also have a setting.json file where I store my S3 keys.

Have a great day and don't forget to smile.


Solution

  • I already managed to fix this. Eventhough I thought my upload-rules.js was in a server folder. It turned out to not be in the right one. After moving it to the right folder it all works.

    So for anyone who has the same problem, double check if your file are in the right place.