在storage.get之后使用Promise发送HTTP请求

时间:2019-04-18 15:36:02

标签: angular ionic3

我正在Ionic3中运行一个应用程序,并且试图通过http请求将设备信息发送到我的API,因此可以将其存储在SQL数据库的表中。

显然,这些任务正在异步运行,并且SEND方法正在争夺storage.get方法(该方法从离子存储中检索信息),因此它以null或空值传递

我在线阅读了一些帖子,并试图将此请求构建为Promise,但是在测试时,它返回以下错误:“无法读取未定义的属性'webservice'”

这是我的代码:

           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吗?有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

经过一些测试和研究,我找到了解决方案:

    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 => {                    

                })
            }
        )
    });
}

这是最终代码,谢谢!

答案 1 :(得分:0)

您也可以通过适当的错误处理和干净的流程尝试这种方法...

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')

就是这样!