Skip to content
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

types.map shows error when using types.number in identifier subtype #191

Closed
boatkorachal opened this issue Jun 14, 2017 · 5 comments
Closed

Comments

@boatkorachal
Copy link

const { types } = require("mobx-state-tree")

const Todo = types.model("Todo", {
    todo_id: types.identifier(types.number),
    title: types.string,
})

const TodoStore = types.model("TodoStore", {
  todos: types.optional(types.map(Todo), {}),
}, {
  addTodo(todo) {
    this.todos.put(todo)
  },
})

const todoStore = TodoStore.create({})

todoStore.addTodo({
  todo_id: 1,
  title: "Test",
})

It shows

Error: [mobx-state-tree] A map of objects containing an identifier should always store the object under their own identifier. Trying to store key '1', but expected: '1'

But when I use types.string as identifier, it's ok

@mattiamanzati
Copy link
Contributor

Hi! Thanks for reporting!
Can you please try to clone, build and install the master branch from the repository and check if it fixes your problem? :)

@boatkorachal
Copy link
Author

Yes! It fix now
Thanks! :)

But I found another bug when I put the same identifier on map.

const Todo = types.model("Todo", {
    todo_id: types.identifier(types.number),
    title: types.string,
})

const TodoStore = types.model("TodoStore", {
  todos: types.optional(types.map(Todo), {}),
}, {
  addTodo(todo) {
    this.todos.put(todo)
  },
})

const todoStore = TodoStore.create({})

todoStore.addTodo({
  todo_id: 1,
  title: "Test",
})

console.log(todoStore.todos.toJSON())
// PRINT: { '1': { todo_id: 1, title: 'Test' } }

todoStore.addTodo({
  todo_id: 1,
  title: "Test Edited",
})

console.log(todoStore.todos.toJSON())
// PRINT: { '': { todo_id: 1, title: 'Test Edited' } }  <-- it shows the key is empty string

console.log(todoStore.todos.get(1).toJSON())
// PRINT: { todo_id: 1, title: 'Test Edited' } <-- but it can get the model that i put

@mattiamanzati
Copy link
Contributor

Thanks for reporting again! :)
Can you please update from the master and check if it's fixed? :)

@boatkorachal
Copy link
Author

Yes! It's fixed now
Thank you very much :)

@mattiamanzati
Copy link
Contributor

You're welcome! Will be released soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants