首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角承诺未定义误差

角承诺未定义误差
EN

Stack Overflow用户
提问于 2017-09-22 14:36:37
回答 2查看 131关注 0票数 0

这是第一次工作承诺,我有这个功能,返回一个承诺。

代码语言:javascript
复制
  public DatesGenerator(futercampaign: ICampaign, searchparam: any, i: number): ng.IPromise<any> {
    return this.$q((resolve, reject) => {
      let num: number;
      let month: number;
      let tempDate: Date;
      let deadline: Date = new Date(searchparam.start_date);
      let periodicity: Date = new Date(searchparam.start_date);

      if (searchparam.periodicity === 'onemonth') { num = (i * 1); month = 1; } else if (searchparam.periodicity === 'twomonth') { num = (i * 2); month = 2; } else if (searchparam.periodicity === 'threemonth') { num = (i * 3); month = 3; }
      if (searchparam.periodicity === '14') {
        resolve(() => {
          futercampaign.start_date = new Date(periodicity.setDate(periodicity.getDate() + (searchparam.periodicity * i)));

          /* Storing the start_date temporarily */
          tempDate = new Date(futercampaign.start_date);

          /* Calculating the End Date */
          futercampaign.end_date = new Date(tempDate.setDate(tempDate.getDate() + searchparam.periodicity));
        })
      } else {
        resolve(() => {
          futercampaign.start_date = new Date(periodicity.setMonth(periodicity.getMonth() + num));

          /* Storing the start_date temporarily */
          tempDate = new Date(futercampaign.start_date);

          /* Calculating the End Date */
          futercampaign.end_date = new Date(tempDate.getFullYear(), tempDate.getMonth() + month, 0);
        })
      }
      /* Calculating the Deadline */
      futercampaign.deadline = new Date(tempDate.setDate(tempDate.getDate() - searchparam.deadline));

      return futercampaign;

    });

  }

并被用于另一种方法。

代码语言:javascript
复制
      public generateCampaigns(campaing: any, searchparam: any, ev: any): void {

        for (let i: number = 0; i < searchparam.number_campaigns; i++) {
          if (validapps[i]) {
            let copy: any = angular.copy(campaign);
            this.DatesGenerator(copy, searchparam, i).then(()=>{
            if (!searchparam.wildcard) {
              copy.application = validapps[i];
              copy.os = validapps[i].os[0];
              copy.version_app = copy.os.version[0];
              copy.campaing_code = validapps[i].application_code;
            }
            this.suggescampaigns.push(copy);
            });
          }
        }
      }

但是当我调用第二个函数时,给出错误,tempDate是没有定义的,我不知道为什么,有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-22 14:52:17

这个错误正好告诉您,tempDate是未定义的,因为当您使用它时,它还没有在这一行中得到解决:

代码语言:javascript
复制
futercampaign.deadline = new Date(tempDate.setDate(tempDate.getDate() - searchparam.deadline));

你应该在解决问题时使用它。这意味着将futercampaign.deadline设置在DatesGenerator的解析中,并返回未竟运动作为承诺的解决方案。

票数 0
EN

Stack Overflow用户

发布于 2017-09-22 15:01:02

原因是,嵌入在要解析的调用中的代码直到函数退出后才会被执行,但是在函数结束之前,您正在访问未初始化的tempDate变量。this.$q()方法中的代码将在“稍后时间”被调用,这很可能是在第二个函数退出之后。

由于代码是同步的,所以我不清楚为什么要对此使用承诺。承诺通常用于可能是异步的代码。你应该去掉所有的承诺包装,你可能会得到你想要的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46367267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档