doushi1974 2019-02-27 00:20
浏览 341
已采纳

在promise axios.all中找不到数据

Problem is that (this.form.tasks neither other data) cannot be found in the promise then() with axios.all. Outside the promise it works but not inside. I get the error :

Uncaught (in promise) TypeError: Cannot read property 'form' of null

        axios.all([
            axios.get('/tasks/'+this.token_proj),
            axios.get('/keywords/'+this.token_proj),
            axios.get('/getTasks/'+this.token_proj),
            axios.get(`/discussion/${this.token_proj}/${this.form.page}`)
        ])
        .then(axios.spread(function (tasksResponse, keywordsResponse,getTasksResponse,discResponse) {

            console.log('tasks', tasksResponse.data);
            console.log('keyword', keywordsResponse.data);
            console.log('getrask', getTasksResponse.data);
            console.log('disc', discResponse.data);

            this.form.tasks = tasksResponse.data;



        }));

The tasks variable is set in data and exist. I use Vuejs and axios vform.

data(){
        return{
            form: new Form({

            tasks:[],
            tasks_management:[],
            discuss:{},
            users:[],
            keywords:[],


            })
        }
    }
  • 写回答

1条回答

  • dowy77780 2019-02-27 00:47
    关注

    Instead of aliasing this consider using the fat arrow function to retain the scope of this inside of the anonymous callback:

    .then(axios.spread((tasksResponse, keywordsResponse,getTasksResponse,discResponse) => {
      //this is now scoped correctly.
    }))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站