控制台报错
老师,在推入新的todo的时候,控制台出现了Uncaught TypeError: Cannot read property 'unshift' of undefined的错误,请问是怎么回事呢?这里我尝试着打印了this.props.store.todos,但是显示为undefined
老师,在推入新的todo的时候,控制台出现了Uncaught TypeError: Cannot read property 'unshift' of undefined的错误,请问是怎么回事呢?这里我尝试着打印了this.props.store.todos,但是显示为undefined
2018-08-19
unshift是js原生数组中的方法,它的作用是向数组的头部添加一个或多个元素。如果你控制台打印出Cannot read property 'unshift' of undefined。说明你的todo根本就不是一个数组。你可以在你的createTodo中将你的todos打印一下看看是什么
@action.bound createTodo(title) { console.log(this.todos); this.todos.unshift(new Todo(title)) }
这是我打印出的信息
Proxy {length: 0, Symbol(mobx administration): ObservableArrayAdministration}
[[Handler]]: Object
[[Target]]: Array(1)
0: Todo {id: 0.21948423547067275, Symbol(mobx did run lazy initializers): true, Symbol(mobx administration): ObservableObjectAdministration$$1}
length: 1
Symbol(mobx administration): ObservableArrayAdministration {owned: false, values: Array(1), proxy: Proxy, lastKnownLength: 1, atom: Atom$$1, …}
__proto__: Array(0)
[[IsRevoked]]: false
举报