Property 'id' does not exist on type 'Promise<unknown>' - http-post

Im having an issue on my code posting my input value to my json api. Please help how can i address this problem on Property 'id' does not exist on type 'Promise'.
Sorry im just a beginner here. Thanks in advance.
//This is my code on component.ts
async OnNext() {
let newCase = {PersonalInformation:{}, Status:"Pending", LastViewedPage:"personal-info",
CreatedBy: this.User.userId}
newCase.PersonalInformation = this.PersonalInformation.value
const responseData = this.conn.CreateCase(newCase)
if(responseData.id) {
this.router.navigate(['address-info'])
}else{
this.errorDesc = "System error."
}
}
//This is my post code in my service definition.
async CreateCase(data: any) {
return new Promise((success, failed) => {
this.http.post('http://localhost:3000/caseList', data).subscribe(
data => {success(data)},
error => {failed(error)}
)
})
}

Related

Angular getting A client-side or network error occurred: Cannot read properties of undefined (reading 'viewContainerRef')

I am using the following forkJoin to make preview call only for attachment that can be previewable. canPreview flag shows that.
But as soon as I add the filter, it is throwing the following error. I am not using viewContaienrRef anywhere in this component. Can anyone help to find what am I doing wrong here?
'A client-side or network error occurred: Cannot read properties of undefined (reading 'viewContainerRef')'
forkJoin(
this.question.attachments
.filter(attachment => attachment.canPreview) //error after adding this line
.map((attachment) => {
const { fileName, attachmentId, canPreview } = attachment;
const attachmentUploadResult: AttachmentUploadResult = {
fileName,
attachmentId,
canPreview
};
return this.uploadService.getBlob(attachmentId).pipe(
map((blob) => {
return {
fileName,
attachmentId,
imageURL: this.sanitizer.bypassSecurityTrustUrl(
URL.createObjectURL(blob)
),
canPreview
} as AttachmentUploadResult;
}),
catchError(() =>
of(attachmentUploadResult)
)
);
})
)
Thanks

error Policy in Apollo Client React does'nt work

I have aproblem when test Apollo.When I try query with apollo and graphql, i want response return error and partical data, so I set property errorPolicy:'all'. But its not work. I don't no why? Help please!
Here my code:
query { animal {
name
age }, school {
name
numberfd } } `
const { loading,data,error} = useQuery(GET_DASHBOARD_DATA, {
errorPolicy:'all',
onCompleted: (res) => {console.log("complete",res)},
onError : (res,data) => {console.log("ERRRR",res,data)},
})
and i want to receive:
{
error:[...], data:[animal:[...]] }
but its only response error.Here is Apollo's doc: https://www.apollographql.com/docs/react/data/error-handling/
onError type is onError?: (error: ApolloError) => void;. You don't have data inside onError callback.
After useQuery you can add:
console.log('data', data)
console.log('error', error)
I faced the same issue with errorPolicy: 'all', I only received the partial result inside onCompleted callback of useQuery, but no errors.
I created an ErrorLink like this:
private createErrorLink = () => {
return new ApolloLink((operation, forward) => {
return forward(operation).map((response) => {
// filter out errors you don't want to display
const errors = filterSomeErrors(response.errors);
if (errors && response?.data) {
response.data.errors = errors;
}
return response;
});
});
};
Now inside my onCompleted callback I get my data as well as errors. You will have to tweak your types a bit, because seems there is no errors field on response.data by default.
Mind that if you use onError from Apollo and return something from the link, it will retry your request containing errors!

please I need help, how can one write a controller for the default user content type in strapi

I tried this from the docs:
async findOne(ctx) {
const { id } = ctx.params;
const entity = await strapi.services.user.findOne({ id });
console.log(entity)
return sanitizeEntity(entity, { model: strapi.models.user });
},
};
and it gave me this error 'error TypeError: Cannot read property 'findOne' of undefined
'

How to call chain subscription (RxJs) in ionic events

I have a BaseDataService class and it has a method for HttpGet requests.
protected Get<TResponse>(
endPoint: string
): Observable<BaseResponse<TResponse>> {
return this.httpClient.get<TResponse>(this.baseUrl + endPoint).pipe(
map(data => {
const response = <BaseResponse<TResponse>>{};
response.Data = data;
response.Errors = [];
response.HasError = false;
return response;
}),
catchError(errors => {
const response = <BaseResponse<TResponse>>{};
response.Errors = [];
response.Errors.push(errors.error);
response.HasError = true;
return of(response);
})
);
}
And I have a LocationDeviceDataService which extends BaseDataService and it has a method for Get LocationDevices
getAll() {
return this.Get<BasePaginatedResponse<LocationDeviceResponse>>(
EndPoints.GET_LOCATIONDEVICES
);
}
And I am calling this method inside event ,
this.events.subscribe("connection-type:wifi", () => {
this.locationDataService.getAll().subscribe(t => {
localStorage.setItem('LOCATION_DEVICES', JSON.stringify(t.Data.items))
});
});
Everything is fine at first call , but when another events (https://ionicframework.com/docs/api/util/Events/)publish for "connection-type:wifi" this.locationDataService.getAll().subscribe returns responses 1x,2x,4x etc. slower.
I am sure for nothing wrong in back-end.
Should unsubscribe or complete subscription ? If I should , I dont have any trigger for that.
Could you please tell me what is wrong in this code ?
I solved my issue.
I think you can not call observable method inside Ionic events so I changed my method to void. Everything is fine for now.

`RxJS` throws error on subcribe when do request

I am making a request using observable. and trying to subcribe the value. But getting error on typescript. any on help me?
I like to do this:
public getCountry(lat,lan):Observable<any>{
return this.http.get(this.googleApi+lat+','+lan+'&sensor=false').subscribe(data => {
return this.genertedData(data);
} );
}
But getting error as follows:
UPDATES
public getCountry(lat,lan):Observable<any>{
return this.http.get(this.googleApi+lat+','+lan+'&sensor=false').map( data => {
data.results.map( array => {
let details = array.address_components.find( obj => obj.types.includes("country") );
this.countryDetails.countryLongName = details.long_name;
this.countryDetails.countryShortName = details.short_name;
})
return this.countryDetails;
})
}
The problem is that your return type states Observable<any>, where as you actually return whatever this.genertedData(data) returns (Hint: Sounds like a typo in your function. Guess it should be called generatedData ?).
Best practice would be to move the http call into a service and subscribe to its returned Observable within your component.
So to speak:
// => service.ts
public getCountryObservable(lat,lan):Observable<any> {
return this.http.get(this.googleApi+lat+','+lan+'&sensor=false');
}
Your component would look something like:
// => component.ts
export class YourComponent {
constructor(public yourService: YourService) {}
getCountry(lat, lan): whateverDataTypeYourGeneratedDataReturns {
this.yourService.getCountryObservable(lat, lan).subscribe((data) => {
this.data = this.generatedData(data);
});
}
}
Since the return type of the function is Observable<any>, I guess it should just return this.http.get(this.googleApi+lat+','+lan+'&sensor=false')

Resources