代码之家  ›  专栏  ›  技术社区  ›  jigarzon

映射结果在axios中。js承诺

  •  0
  • jigarzon  · 技术社区  · 7 年前

    我想在axios返回的Promise中执行映射操作。

    它可能是这样的:

      this.$http.get('/movements', this.$auth.httpConfig())
      .map(row => {
        return {
          'origin' : row.origin ? row.origin.name : '',
          'destination' : row.destination ? row.destination.name: '',
          'type_code' : row.type.code,
          'type' : row.type.type,
          'amount' : row.amount,
          'description' : row.description,
          'movement_date' : row.movement_date,
          'id': row.id
        }
      }).
      then((response) => {
        this.table = response.data;
      })
    

    当然,这不起作用,因为map函数不包括在Promise中,它需要一些额外的库,比如Bluebird,但我在将其与axios集成时没有成功。

    1 回复  |  直到 7 年前
        1
  •  0
  •   jigarzon    7 年前

    该解决方案比使用另一个库更简单。谢谢@Bergi和@Ixe。

      this.$http.get('/movements', this.$auth.httpConfig()).
      then(response => {
        this.table = response.data.map((row) => {
          return {
            'origin': row.origin ? row.origin.name : '',
            'destination': row.destination ? row.destination.name : '',
            'type_code': row.type.code,
            'type': row.type.type,
            'amount': row.amount,
            'description': row.description,
            'movement_date': row.movement_date,
            'id': row.id
          }
        });
      });