简体   繁体   中英

Ember RSVP.Promise resolve function return undefined

I have function in my adapter "findQuery" the function need to GET data from the server and return it.

the function:

findQuery: function(store, type, query) {
  var userId = query['user_id'];
  delete query['user_id'];

  if (this.sortQueryParams) {
    query = this.sortQueryParams(query);
  }

  var adapter = this;
  // Create nested url
  //return this.ajax(this.urlPrefix(this.pathForType(type.typeKey), this.buildURL(type, userId)), 'GET', { data: query });
  // We would like to relate the rows to the connected user
  return new Ember.RSVP.Promise(function(resolve, reject) {
    adapter.ajax(adapter.buildURL(type.typeKey, null, userId), 'GET', { data: query }).then(function(devices) {
      store.find('user', userId).then(function(user) {
        devices.setEach('user_id', user);
        resolve(devices);  // devices is set exactly as i wanted!
      }, function(failed) {
        reject(failed);
      });

    }, function(error) {
      reject(error);
    });
  });
}

So when I debug the function Ii see that the function get into the resolve with the correct data (the API returned all the requested data as Array of objects). And then I get log Error:

Error: Assertion Failed: The response from a findQuery must be an Array, not undefined

UPDATE

The issue has been solved but I still don't understand why it occurs.

what happen?

I set in store-injector file

 inflector.irregular('general_device', 'devices');

That resolve for me the api endpoints that look like that:

https://api.url.net/account/85/devices

While in some way it has some conflict, what I tried is to resolve an url with the above function that look like that:

https://api.url.net/account/85/users/135/devices

It solved but I can't find references why it happened, it make a sense but still I hope that someone could explain the reason for it to happen.

You need to return a PromiseArray.

See the documentation on an example of how to use it.

In you example, it would be changed to:

findQuery: function(store, type, query) {
  var userId = query['user_id'];
  delete query['user_id'];

  if (this.sortQueryParams) {
    query = this.sortQueryParams(query);
  }

  var adapter = this;
  // Create nested url
  //return this.ajax(this.urlPrefix(this.pathForType(type.typeKey), this.buildURL(type, userId)), 'GET', { data: query });
  // We would like to relate the rows to the connected user

  var promise = new Ember.RSVP.Promise(function(resolve, reject) {

    adapter.ajax(adapter.buildURL(type.typeKey, null, userId), 'GET', { data: query }).then(function(devices) {
      store.find('user', userId).then(function(user) {
        devices.setEach('user_id', user);
        resolve(devices);  // devices is set exactly as i wanted!
      }, function(failed) { reject(failed); });

    }, function(error) { reject(error); });

  });

  return DS.PromiseArray.create({
    promise: promise
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM