Skip to content

[Ember] Uploads service is undefined #301

@styletabtech

Description

@styletabtech

Hey @berziiii I'm working on creating a file upload via your instructions (super easy to follow btw thank you!!) and I've run into an issue where 'uploads' is undefined in the route of my new-photo

new-photo/route.js

export default Ember.Route.extend({

  actions: {
    createPhoto(newPhoto) {
        console.log('newPhoto is', newPhoto); 
        console.log(this.get('uploads'));
        return this.get('uploads').newPhotoUpload(newPhoto) // this is where uploads is undefined
       .then(() => this.transitionTo('photos'))
       .catch((error) => console.error(error));
    },
  },
});

I'm not sure if I need to inject this service in a particular way? I've tried doing so in the above file but Ember said no. I'm a little lost on how to go about debugging past this point now that I've identified the problem.

Here's the rest of my code:

new-photo-form/template:

<form enctype="multipart/form-data" id="new-photo-form" {{ action 'create' on='submit' }}>
  <fieldset>
    <legend>Add A Photo</legend>
    <input type="text" class="form-control" name="photo[file_name]" placeholder="Image Name">
    <input type="file" name="photo[photo_upload]" value="Upload Photo">
    <input type="submit" class="btn btn-success" name="submit" value="Add Photo">
  </fieldset>
</form>

new-photo-form/component:

export default Ember.Component.extend({
  newPhoto: {}, // New photo object being constructed from the form

  actions: {
    create() {
      let newPhoto = new FormData(Ember.$('#new-photo-form')); // Ember jQuery to find the form by it's id.
      this.sendAction('create', newPhoto); // Grabs the newPhoto object which has been converted into FormData and sends it up to the route template
      this.set('newPhoto', null); // Resets the newPhoto Object
    }
  }
});

new-photo/template

{{new-photo-form create='createPhoto'}}

photo/uploads/service.js:

export default Ember.Service.extend({
  ajax: Ember.inject.service(),

    newPhotoUpload (newPhoto) {
      return this.get('ajax').post('/photos', {  
        data: newPhoto, // The FormData object which we have called `newPhoto`
        contentType: false,
        processData: false,
      });
     },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions