“[__array_observer__: ModifyArrayObserver]” 遍历数组时
"[__array_observer__: ModifyArrayObserver]" when iterating over an array
这里是第一个问题,如果我没有包含所有需要的内容,请告诉我。
我有一个名为 this.tickets 的数组存储在我的构造函数中,如下所示:
constructor(TicketService) {
this.ticketService = TicketService;
this.customers;
this.tickets = [];
this.status = false;
}
我有一个函数可以进行 api 调用,并在检查数据是否存在于我的本地数据库中后一次推送一个响应数据。
setTickets () {
this.customers.forEach(data => {
var acctName = encodeURIComponent(data.accountname);
this.ticketService.getTicketByAccount(acctName).then(resp => {
if (resp.data.accountname == data.accountname) {
this.tickets.push(resp.data)
}
})
})
}
之后我尝试在一个单独的函数中遍历数组:
getZendeskTicketStatus () {
console.log(this.tickets)
console.log("this is the arrys length: " + this.tickets.length)
for (var i = 0; i < this.tickets.length; i++) {
console.log(this.tickets[i])
}
}
我收到以下回复:
[__array_observer__: ModifyArrayObserver]
this is the arrys length: 0
我在承诺中调用这些函数中的每一个,以便它们按顺序发生。
activate() {
new Promise((resolve, reject) => {
var customerPromise = this.setCustomers();
resolve(customerPromise);
console.log("First")
})
.then(() => {
console.log("Second")
this.setTickets();
})
.then(() => {
console.log("Third")
this.createTicketsInDb();
this.getZendeskTicketStatus();
})
}
我不明白这个 array_observer 是什么或者我如何才能成功地迭代这个数组。
如有任何帮助,我们将不胜感激。
提前谢谢你。
试着理解这个例子,希望它能澄清
所以如果我调用 setTicket 并不意味着它会等待响应。
所以为了让 setTickets 等待响应我做了这个修改
setTickets () {
return new Promise((resolve, reject)=>{
this.customers.forEach(data => {
var acctName = encodeURIComponent(data.accountname);
this.ticketService.getTicketByAccount(acctName).then(resp => {
if (resp.data.accountname == data.accountname) {
this.tickets.push(resp.data)
}
resolve();
})
})
})
}
现在要求调用函数等待响应
activate() {
new Promise((resolve, reject) => {
var customerPromise = this.setCustomers();
resolve(customerPromise);
console.log("First")
}).then(async () => {
console.log("Second");
//dont skip it till the time you get the response
await this.setTickets();
}).then(() => {
console.log("Third");
this.createTicketsInDb();
this.getZendeskTicketStatus();
})
}
希望这能说明问题。
这里是第一个问题,如果我没有包含所有需要的内容,请告诉我。
我有一个名为 this.tickets 的数组存储在我的构造函数中,如下所示:
constructor(TicketService) {
this.ticketService = TicketService;
this.customers;
this.tickets = [];
this.status = false;
}
我有一个函数可以进行 api 调用,并在检查数据是否存在于我的本地数据库中后一次推送一个响应数据。
setTickets () {
this.customers.forEach(data => {
var acctName = encodeURIComponent(data.accountname);
this.ticketService.getTicketByAccount(acctName).then(resp => {
if (resp.data.accountname == data.accountname) {
this.tickets.push(resp.data)
}
})
})
}
之后我尝试在一个单独的函数中遍历数组:
getZendeskTicketStatus () {
console.log(this.tickets)
console.log("this is the arrys length: " + this.tickets.length)
for (var i = 0; i < this.tickets.length; i++) {
console.log(this.tickets[i])
}
}
我收到以下回复:
[__array_observer__: ModifyArrayObserver]
this is the arrys length: 0
我在承诺中调用这些函数中的每一个,以便它们按顺序发生。
activate() {
new Promise((resolve, reject) => {
var customerPromise = this.setCustomers();
resolve(customerPromise);
console.log("First")
})
.then(() => {
console.log("Second")
this.setTickets();
})
.then(() => {
console.log("Third")
this.createTicketsInDb();
this.getZendeskTicketStatus();
})
}
我不明白这个 array_observer 是什么或者我如何才能成功地迭代这个数组。
如有任何帮助,我们将不胜感激。
提前谢谢你。
试着理解这个例子,希望它能澄清 所以如果我调用 setTicket 并不意味着它会等待响应。 所以为了让 setTickets 等待响应我做了这个修改
setTickets () {
return new Promise((resolve, reject)=>{
this.customers.forEach(data => {
var acctName = encodeURIComponent(data.accountname);
this.ticketService.getTicketByAccount(acctName).then(resp => {
if (resp.data.accountname == data.accountname) {
this.tickets.push(resp.data)
}
resolve();
})
})
})
}
现在要求调用函数等待响应
activate() {
new Promise((resolve, reject) => {
var customerPromise = this.setCustomers();
resolve(customerPromise);
console.log("First")
}).then(async () => {
console.log("Second");
//dont skip it till the time you get the response
await this.setTickets();
}).then(() => {
console.log("Third");
this.createTicketsInDb();
this.getZendeskTicketStatus();
})
}
希望这能说明问题。