mobx-state-tree: Cannot resolve a reference with multiple candidates (two model instances with same identifier)

Bug report

  • I’ve checked documentation and searched for existing issues
  • I’ve made sure my project is based on the latest MST version
  • Fork this code sandbox or another minimal reproduction.

Sandbox link or minimal reproduction code Sandbox example

Describe the expected behavior I have a Base model that is composed in two places one, two in the store tree. This model uses nested models Country and City. I add a new country and a new city in both places with the same identifiers. The cities are also placed in an array of references at the country model. Now I want to get the cities of a country by reading the list of cities references.

Describe the observed behavior When trying to get the list of cities from references list of Country model, the following error throws:

[mobx-state-tree] Cannot resolve a reference to type 'late(function () {
    return City;
  })' with id: '1000' unambigously, there are multiple candidates: /one/cities/1000, /two/cities/1000

The same is NOT happening when trying to get a country, even though there are multiple countries with the same identifier, but not in a list of references.

It seams that resolving an item by its reference identifier not working if there are multiple items with the same identifier.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

This issue has been automatically marked as stale because it has not had recent activity in the last 10 days. It will be closed in 4 days if no further activity occurs. Thank you for your contributions.

I’ve released a new library to help with this problem, which I think is pretty cool, well-tested, and pretty well documented.

Please check it out and let me know what you think about it!

It’s called mst-reference-pool and you can find it here:

https://github.com/infinitered/mst-reference-pool

In a nutshell, it gives you tools to easily create a pool of instances, and then you simply use references from anywhere on the tree. It also comes with a garbage collector so you don’t end up with a memory leak when instances are no longer referenced from anywhere.

I wouldn’t mind a more elegant solution to this issue, as it’s something we’ve run into ourselves. Re-opening for future consideration.