Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 转换承诺<;任何>;到特定类型错误_Angular_Typescript_Ionic4 - Fatal编程技术网

Angular 转换承诺<;任何>;到特定类型错误

Angular 转换承诺<;任何>;到特定类型错误,angular,typescript,ionic4,Angular,Typescript,Ionic4,输入脚本/angular4+/ionic4的新手问题。我实现了一个服务,该服务将进行后端REST调用,并根据调用的响应将获得的信息存储到本地存储器中。 原因是它位于本地存储中,以便我可以在稍后的时间点查询相同的内容。我似乎遇到了类型转换错误。任何线索都会有帮助 this.locationService.saveLocationNew(this.newLocation).subscribe(res=> { this.locationInfo = res; console

输入脚本/angular4+/ionic4的新手问题。我实现了一个服务,该服务将进行后端REST调用,并根据调用的响应将获得的信息存储到本地存储器中。 原因是它位于本地存储中,以便我可以在稍后的时间点查询相同的内容。我似乎遇到了类型转换错误。任何线索都会有帮助

 this.locationService.saveLocationNew(this.newLocation).subscribe(res=> {
     this.locationInfo = res;
     console.log('Response from backend'+ this.locationInfo);
     this.storage.set(StorageKeys.LOCATION_DATA, this.locationInfo);
     this.locationInfo = null;
     this.locationInfo = <AddLocationResponse>this.storage.get(StorageKeys.LOCATION_DATA); --> this is where I see the type conversion error.
   });
this.locationService.saveLocationNew(this.newLocation).subscribe(res=>{
this.locationInfo=res;
console.log('Response from backend'+this.locationInfo);
this.storage.set(StorageKeys.LOCATION\u数据,this.locationInfo);
this.locationInfo=null;
this.locationInfo=this.storage.get(StorageKeys.LOCATION_DATA);-->这就是我看到类型转换错误的地方。
});
我的服务是这样的

 saveLocationNew(request: AddLocationData): Observable<AddLocationResponse> {
      return this.httpClient.post<AddLocationResponse>(this.addLocationUrl, request , { headers: Constants.createHeader()}); --> I don't use a then and map res to res.json ; since that is not needed in Ionic4
 }
saveLocationNew(请求:AddLocationData):可观察{
返回this.httpClient.post(this.addLocationUrl,request,{headers:Constants.createHeader()});-->我不使用then和映射res到res.json;因为在Ionic4中不需要这样做
}

检查是否存在类型错误

function determineIfIsAnimalOrHuman(toBeDetermined: PersonOrAnimal): toBeDetermined is Animal {
  if((toBeDetermined as Animal).type){
    // do some thing
  }
   throw new TypeError('Object is no Animal');
}
试试这个

 this.locationService.saveLocationNew(this.newLocation).subscribe(async (res)=> {
     this.locationInfo = res;
     console.log('Response from backend'+ this.locationInfo);
     this.storage.set(StorageKeys.LOCATION_DATA, this.locationInfo);
     this.locationInfo = null;
     const res = await <AddLocationResponse>this.storage.get(StorageKeys.LOCATION_DATA).catch((err) => 'return anything you want');
     if (res) {
       this.locationInfo = res;
     }
   });
this.locationService.saveLocationNew(this.newLocation).subscribe(异步(res)=>{
this.locationInfo=res;
console.log('Response from backend'+this.locationInfo);
this.storage.set(StorageKeys.LOCATION\u数据,this.locationInfo);
this.locationInfo=null;
const res=wait this.storage.get(StorageKeys.LOCATION_DATA).catch((err)=>“返回您想要的任何内容”);
如果(res){
this.locationInfo=res;
}
});

我看到了这个错误:将类型“Promise”转换为类型“AddLocationResponse”可能是一个错误,因为两种类型都没有足够的重叠。如果这是有意的,请首先将表达式转换为“未知”。这似乎对我有效,但这是异步的:this.storage.get(StorageKeys.LOCATION_DATA)。然后((val)=>{this.locationData=val;});有没有办法从异步调用到同步?我意识到最好将所有这些调用都放在ionViewLoad()中,以便我需要进行的一些本地存储查询可以更早地使用