我有以下对象文字,我试图在存储对象(使用 localForage)和视图对象之间传递一个 todoList 数组。
const store = {
setUpStore() {
this.todos = localforage.createInstance({
name: 'myApp',
storeName: 'todos'
});
this.completed = localforage.createInstance({
name: 'myApp',
storeName: 'completed'
});
},
// Code omitted for brevity
get todoList() {
const todoList = [];
this.todos.iterate((value, key, iterationNumber) => {
let item = {id: key, title: value.title};
if (value.prioritized === true) {
todoList.unshift(item);
} else {
todoList.push(item);
}
}).then(() => {
console.log('Got todo list');
}).catch((err) => {
console.log(`There was an error: ${err}`);
});
return todoList;
},
}
const view = {
// Code omited for brevity
displayTodos() {
todoList = store.todoList;
console.log(todoList); // This logs what appears to be an array
todoList.forEach((item) => {
// This doesn't work
console.log(item.title);
});
}
}
当我在控制台中调用 store.todoList getter 时,我得到一个可以使用的数组。view.displayTodos() 方法中的 console.log(todoList) 似乎可以工作,但在 view 方法中调用 forEach() 或对 todoList 执行任何其他类型的数组操作不起作用。这里发生了什么?