【发布时间】:2019-07-23 03:56:56
【问题描述】:
我正在尝试使用名为 addDevice() 的函数更新本地存储,它会检查密钥中是否已经存在一些数据,然后将其附加到其他内容中,只需更新密钥即可。
export class DeviceService implements devicesInterface {
devices : Array<deviceInterface> = [];
constructor(private storage : Storage, private http : HttpClient) { }
addDevice(ssid : String, name : String){
console.log("add device called "+ssid +name)
return this.getDevices().then((res : devicesInterface) => {
console.log("res");
console.log(res)
if (res) {
this.devices = res.devices;
console.log(res);
this.devices.push({ ssid: ssid, name: name });
return this.storage.set(TOTAL_DEVICES, { devices: this.devices });
}
else {
console.log("not res");
let devices_obj = { devices: [{ ssid: ssid, name: name }] };
return this.storage.set(TOTAL_DEVICES, devices_obj);
}
})
}
getDevices(){
return this.storage.get(TOTAL_DEVICES)
}
}
从页面中,我调用了一个 API,它解析为 deviceInterface 类型的数组。
this.deviceService.getDevicesFromServer(this.authenticationService.getToken())
.subscribe((res : Array<deviceInterface>) =>{
if(res){
this.storage.remove("TOTAL_DEVICES")
this.devices = []
}
res.map(async device => {
await this.deviceService.addDevice(device.ssid, device.name).then(res=>{
console.log("device added..")
this.devices.push(device)
console.log(res)
})
});
})
addDevice 函数必须在链中调用,这样下次调用时它会获取数据并追加,如果异步调用此函数,数据将被一次又一次地覆盖。
我已经搜索了很多但没有找到任何与我的问题相匹配的相关解决方案,我如何在 res 对象上链接迭代并在最后一次调用解决后调用 addDevice 函数。
【问题讨论】:
-
您确定您的地图功能正在做您认为的事情吗?看看这个答案stackoverflow.com/a/37576787/4553162,看看你不能真正做一个这样的循环,并期望它等待每次迭代完成,然后再触发下一次对
addDevice的调用 -
一点代码可能会有很大帮助,我认为 map 不应该用作迭代器。
-
当然,我将添加一个示例作为我建议的答案
标签: javascript angular typescript ionic-framework promise