Angular 使用promise在storage.get之后发送http请求
我正在Ionic3中运行一个应用程序,我试图通过http请求将设备信息发送到我的API,这样我就可以将它存储在SQL数据库的一个表中 显然,任务是异步运行的,SEND方法在storage.get方法(从离子存储检索信息)上运行,因此它以null或空的形式传递 我在网上读了一些帖子,试图将此请求构建为承诺,但在测试时,它返回以下错误:“无法读取未定义的属性'webservice'” 这是我的密码:Angular 使用promise在storage.get之后发送http请求,angular,ionic3,Angular,Ionic3,我正在Ionic3中运行一个应用程序,我试图通过http请求将设备信息发送到我的API,这样我就可以将它存储在SQL数据库的一个表中 显然,任务是异步运行的,SEND方法在storage.get方法(从离子存储检索信息)上运行,因此它以null或空的形式传递 我在网上读了一些帖子,试图将此请求构建为承诺,但在测试时,它返回以下错误:“无法读取未定义的属性'webservice'” 这是我的密码: public sendDeviceInfo(company: string,
public sendDeviceInfo(company: string, worker: string)
{
this.storage.get(StorageEnum.PUSH_NOTIFICATIONS_PLAYERID).then(playerId => {
const getDeviceInfo = new Promise((resolve, reject) => {
if(playerId != null && playerId != '' && typeof playerId != 'undefined') {
let param = {
company : company,
worker : worker,
device : {
uuid : playerId
,model : this.device.model
,manufacturer : this.device.manufacturer
,platform : this.device.platform
,version : this.device.version
}
};
resolve(param)
} else {
const err = new Error('Algo deu errado');
reject(err)
}
})
const sendData = function(param) {
let deviceInfoObject = this.webservice.saveDeviceInfo(JSON.stringify(param)).subscribe(response => {
alert(JSON.stringify(response));
this.storage.set(StorageEnum.DEVICE_INFO, param);
}, err => {
alert(JSON.stringify(err));
})
return Promise.resolve(deviceInfoObject)
}
const deviceInfo = function() {
getDeviceInfo.then(sendData)
.then(function(fulfilled) {
alert(JSON.stringify(fulfilled))
})
.catch(err => alert(err.message))
}
deviceInfo();
});
}
我只想在完成storage.get后发送设备信息,我应该使用Promise吗?有人能帮我吗?经过一些测试和研究,我想出了一个解决方案:
public sendDeviceInfo(company: string, worker: string)
{
this.storage.get(StorageEnum.PUSH_NOTIFICATIONS_PLAYERID).then(playerId => {
let getDeviceInfo = new Promise((resolve, reject) => {
if(playerId != null && playerId != '' && typeof playerId != 'undefined') {
let param = {
company : company,
worker : worker,
device : {
uuid : playerId
,model : this.device.model
,manufacturer : this.device.manufacturer
,platform : this.device.platform
,version : this.device.version
}
};
resolve(param)
} else {
const err = new Error('Algo deu errado');
reject(err)
}
}).then((param) => {
this.webservice.saveDeviceInfo(JSON.stringify(param)).subscribe(response => {
this.storage.set(StorageEnum.DEVICE_INFO, param);
}, err => {
})
}
)
});
}
这是最后的代码,谢谢 通过正确的错误处理和干净的流程,您也可以尝试这种方法
public sendDeviceInfo(company: string, worker: string){
this.storage.get(StorageEnum.PUSH_NOTIFICATIONS_PLAYERID)
.then((playerId)=>{
if(playerId == null || playerId == '' || typeof playerId == 'undefined') {
throw new Error("Algo deu errado");
}
let param = {
company : company,
worker : worker,
device : {
uuid : playerId
,model : this.device.model
,manufacturer : this.device.manufacturer
,platform : this.device.platform
,version : this.device.version
}
}
return param
})
.then((param)=>{
return new Promise((resolve, reject) => {
this.webservice.saveDeviceInfo(JSON.stringify(param))
.subscribe((response)=>{
console.log(response);
resolve(param)
},
(err)=>{
reject(err)
})
});
})
.then((param)=>{
return this.storage.set(StorageEnum.DEVICE_INFO, param)
})
.catch((error)=>{
console.log('error', error)
})
}
this.sendDeviceInfo('company', 'worker')
就这样
this.storage
代表什么?看起来你必须答应这个请求。我更新了我的最新测试,并向你添加了承诺it@Matheus巴蒂斯塔:你能用正确的错误处理和干净的流程来尝试上述方法吗?请告诉我它是否适合你