How to refresh or modify the result of query

Hi,

I’m currently migrating from Ember v2.15 to v3.1 and I’m facing an issue I don’t really know how to address. In several of my routes, the model is the result of a call to query like this:

model() {
  return this.get('store').query('article', { param1: 42, param2: 123 })
}

The problem is that I want to be able to create or delete items from the returned list:

Error: The result of a server query (…) is immutable.

Apparently now the query result is immutable but I can’t find any solution when searching for this topic.

I found a Github issue were calling .toArray() was proposed but I tried and for some reason it also modifies the records so that they don’t even respond to .get() anymore.

I also tried to call model.reload() without any positive outcome. I tried to transition to the same route but since the parameter did not change the model hook was not called again. The error message does not provide any guidance on how to address this kind of situation and that’s pretty frustating.

Changing the query for a findAll is not an option in this case or I’d do it.

Does anyone know of a clean and reliable way to make all of this work?

Thanks!

I found a solution that seems clean enough to me! After posting this message, I went on to read the code of Ember Data and found RecordArray that seemed to be the type for the query result. In the record-array.js file I found the update() method, tried it and it works!

So now after adding or removing a record all I have to to is

this.get('model').update();

I’ll leave it here if anyone else faces this issue, here is the solution.

Cheers!

1 Like

Hey Simon,

Indeed, calling update on the result of store.query is probably the best way to go about it. You can also call route.refresh (documented here.), which re-fires the model hooks for the route you’re calling it from.

2 Likes

Hi @balint, thanks for the tip :slight_smile:

If you’re looking to manipulate your results and don’t want to refresh from the server, call model.toArray() to get the contents and then manipulate that by adding or removing records.