未捕获(在承诺中)TypeError:_this.tasks.push不是函数

时间:2018-01-28 20:33:44

标签: javascript vue.js axios

我一整天都在网上冲浪......帮助。我有Vue.js应用程序。在我的组件中,我将数据数组变量等同于来自服务器的响应。但是当我尝试将一些数据推送到数组时,会出现以下错误:未捕获(在承诺中)TypeError:_this.tasks.push不是函数。我试图注销响应,它返回了这个:

{__ob__: Observer}
8
:
(...)
9
:
(...)
15
:
(...)
__ob__
:
Observer {value: {…}, dep: Dep, vmCount: 0}
get 8
:
ƒ reactiveGetter()
set 8
:
ƒ reactiveSetter(newVal)
get 9
:
ƒ reactiveGetter()
set 9
:
ƒ reactiveSetter(newVal)
get 15
:
ƒ reactiveGetter()
set 15
:
ƒ reactiveSetter(newVal)
__proto__
:
Object

这是我的组件js代码

import User from '../../services/UserService';
    export default {
        data() {
            return {
                taskText: '',
                tasks: []
            }
        },
        methods: {
            createNewTask() {
                User.createUserTask(this.taskText).then(response => {
                    if (response) {
                        this.tasks.push(response);
                        this.taskText = '';
                        Materialize.toast('Task was added', 4000);
                    }
                });
            },
            markTaskAsDone(taskId) {
                User.markTaskAsDone(taskId).then(response => {
                    if (response) {
                        this.tasks = this.tasks.filter(task => task.id !== taskId);
                        Materialize.toast('Task was completed', 4000);
                    }
                });
            }
        },
        mounted() {
            const toast = Materialize.toast('Loading...');
            User.getUserTasks().then(response => {
                if (response) {
                    this.tasks = response;
                    toast.remove();
                    console.log(response);
                }
            });
        }
    }

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我可以做的唯一推断是response在此均衡上不是数组

this.tasks = response;