When overriding buildURL this._super() returns undefined

Hey there!

I’m trying to make posts to my Django backend and I did some researches about it and found that Django requires a / at the end of post requests’ URLs. The URL would be something like this: http://localhost:8000/users/.

I’m using the JSONAPIAdapter and I know that the post requests doesn’t have the /. That’s why I’m trying to override the adapter’s buildURL method to fit the requests to the backend.

I’m doing something like this inside my JSONAPIAdapter:

//app/adapters/application.js

...

buildURL(modelName, id, snapshot, requestType, query){
    if (requestType === 'createRecord') {
      return this._super(...arguments) + '/';
    } else {
      this._super(...arguments);
    }
  }

...

But the this._super(...arguments) is always returning undefined. As far as I know it was supposed to call the parent’s buildURL method. Right?

I don’t know if I’m good with this approach because I’m also migrating to Ember Octane. Did I missed something about the this._super(...arguments)?

How can a get the overrided buildURL method to work?

Thank you.

Hello. I was wondering if your adapter currently uses a native JS class. If so, can you try calling super.buildURL(...arguments) instead?

I think you may need a return statement for the else case as well.

Thank you so much! It worked like a charm! About the return statement it was just a typo while I was writing this post. The full adapter now looks like this:

import JSONAPIAdapter from '@ember-data/adapter/json-api';
import ENV from 'gestao-hotelaria-dashboard/config/environment';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';

export default class ApplicationAdapter extends JSONAPIAdapter {
  @service session;
  host = ENV.APP.api;

  @computed('session.data.authenticated.access_token')
  get headers() {
    let headers = {};
    if (this.session.isAuthenticated) {
      headers['Authorization'] = `Token ${this.session.data.authenticated.access_token}`;
    }

    return headers;
  }


  buildURL(modelName, id, snapshot, requestType, query){
    if (requestType === 'createRecord') {
      return super.buildURL(...arguments) + '/';
    } else {
      return super.buildURL(...arguments);
    }
  }
}

Thank you again! :grinning:

1 Like