Ошибка: [mobx-state-tree] Ошибка при конвертации «Модель» - PullRequest
0 голосов
/ 14 апреля 2020

Снимок с сервера не содержит идентификатора, но он мне нужен для ссылок внутри дерева, поэтому я добавил snapshotProcessor, но он не работает для карт:

const Y = types.snapshotProcessor(
  types.model('Y', {
    id: types.identifier,
  }),
  {
    preProcessor(snapshot) {
      return { id: snapshot.id ? snapshot.id : 'y1' };
    },
  },
);

const X = types.model('X', {
  ys: types.map(Y),
}).actions(self => ({
  addIdentifier(id) {
    self.ys.put({ id });
  },
  addElement(y) {
    self.ys.put(y);
  },
}));

x.addIdentifier('y1'); // works
x.addIdentifier();
// Error: [mobx-state-tree] Error while converting <Y@<root>(id: y1)> to `Y`:
//
//    value of type Y: <Y@<root>(id: y1)> is not assignable to type: `Y`, 
// expected an instance of `Y` or a snapshot like 
// `snapshotProcessor({ id: identifier })` instead. 
// (Note that a snapshot of the provided value is compatible with the targeted type)

const y = Y.create({});
x.addElement(y); // the same error

mobx состояние дерева v3.15.0

...