-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get missing reference identifier #641
Comments
what do you mean by yet to be loaded? |
I meant that the model with the identifier that the reference points to is not yet in the tree. Here's a toy example: // setup types
const Person = types.model({
id: types.identifier(types.string),
name: types.string
});
const Cat = types.model({
id: types.identifier(types.string),
owner: types.reference(Person)
});
const Container = types.model({
cat: Cat,
person: Person
});
// database helper functions
const loadCat = (id) => {
//return Cat model
};
const loadPerson = (id) => {
//return Person model
};
// load cat from data store
/*
{
id: "cat_id_1",
owner: "person_id_1"
}
*/
const cat = await loadCat("cat_id_1");
// now load the owner
const person = await loadPerson(cat.owner); // throws
// create complete tree for use in program
const container = Container.create({
cat, person
}); The workaround I am currently using is: const person = await loadPerson(getSnapshot(cat).owner); What I'm hoping for is that there's something like this which I'm missing: const person = await loadPerson(cat.owner.reference);
// or
const person = await loadPerson(getReference(cat.owner)); I would like to be able to get the string 'person_id_1' out of the 'cat' model without having to create a snapshot of the whole model. I could get the db helper functions to return plain objects instead of models, but creating the models inside those functions means that they can have the responsibility of handling the error case where the database doesn't match the model (e.g. someone messed up running db migrations) rather than having to consider that case in every place those helpers are called. Thanks for your help. |
I think what you're looking for is this (custom references):
|
I'm not sure that it is; I don't want to load the data, I just want to get the identifier the reference is looking for. I've read through the source and come up with this: const getReference = (model, property) =>
model.$mobx.values[property].value.storedValue.value; which seems to work. But I guess there are no guarantees that these property names won't change in future versions. Is there any chance of adding such a function to the supported API? |
i see - you’d need a getIdentifier(thing) fn exposed. we just had that request today :) we have it already internally and should expose it @mweststrate |
@robinfehr agreed, let's expose it! |
Closed in favor of #674 |
I'm sorry for not realising that earlier but what about using e.g. const Person = types.model({
id: types.identifier(types.string),
name: types.string
});
const Cat = types.model({
id: types.identifier(types.string),
owner: types.reference(Person)
}).actions(self => ({
afterCreate() {
const {owner} = getSnapshot(self)
...
}
}); |
hey @robinfehr, I am facing a similar need. In my particular, I would like to set the reference on demand (not in When I use |
@TimHollies workaround is the only thing that has worked for me. As @vmurillo mentioned, |
To make things worse, the exception is unavoidable on a
|
Apologies if I've missed an obvious way to do this - but I'm a bit stuck.
I have a reference in a model which links to another model which is yet to be loaded. Attempting to dereference the property (correctly) throws an error. Is there an easy way to access the identifier the reference is looking for.
E.g. Attempting to access the property throws:
Error: [mobx-state-tree] Failed to resolve reference of type EntityType: 'd584fa4e-fb7a-4875-8048-3870118fcdad'
. I want to access the valued584fa4e-fb7a-4875-8048-3870118fcdad
.I've had a few ideas about how to do this:
These all seem a bit overkill to retrieve a string value - is there an API function I'm missing?
The text was updated successfully, but these errors were encountered: