Angular Promises 未定义的错误
Angular Promises undefined Error
这是第一次工作承诺我有这个功能return一个承诺。
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;
});
}
并已将其用于其他方法。
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 未定义,我不知道为什么,有人可以帮助我。
错误正是告诉你的,tempDate 是未定义的,因为当你使用它时,这一行还没有解决:
futercampaign.deadline = new Date(tempDate.setDate(tempDate.getDate() - searchparam.deadline));
你应该在解决后使用它。这意味着在 DatesGenerator 的 resolves 中设置 futercampaign.deadline
并返回 futercampaign 作为分辨率的承诺。
原因是因为嵌入在 resolve 调用中的代码直到您的函数退出后才会执行,但是您在函数末尾访问未初始化的 tempDate 变量,然后再对其进行任何初始化。 this.$q()
方法中的代码将在 "some later time" 处调用,这很可能是在第二个函数退出之后。
由于代码是同步的,我不清楚您为什么为此使用承诺。 Promises 通常用于可能是异步的代码。你应该只删除所有的承诺包装,你可能会得到你正在寻找的东西。
这是第一次工作承诺我有这个功能return一个承诺。
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;
});
}
并已将其用于其他方法。
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 未定义,我不知道为什么,有人可以帮助我。
错误正是告诉你的,tempDate 是未定义的,因为当你使用它时,这一行还没有解决:
futercampaign.deadline = new Date(tempDate.setDate(tempDate.getDate() - searchparam.deadline));
你应该在解决后使用它。这意味着在 DatesGenerator 的 resolves 中设置 futercampaign.deadline
并返回 futercampaign 作为分辨率的承诺。
原因是因为嵌入在 resolve 调用中的代码直到您的函数退出后才会执行,但是您在函数末尾访问未初始化的 tempDate 变量,然后再对其进行任何初始化。 this.$q()
方法中的代码将在 "some later time" 处调用,这很可能是在第二个函数退出之后。
由于代码是同步的,我不清楚您为什么为此使用承诺。 Promises 通常用于可能是异步的代码。你应该只删除所有的承诺包装,你可能会得到你正在寻找的东西。