0

This is WORKING service class for which return single transaction

getEditTransactionById(id: number): Observable<ITransaction> {
    return this.httpClient.get<Result<ITransaction>>(baseUrl'/Transaction/GetEditTransactionById'+ `/${id}`)       
    .pipe(
        map((res)=>{
            res.data.valueDate = new Date(res.data.valueDate);       
            return res.data;
        })
    )
}

Actually how I need to change the date format of fields valuedate in array coming from API

Could you please help?

This is my service class which returns array ...Need to change the date format

getTransactions(): Observable<ITransactionResponse[]> {
    return this.httpClient.get<Result<ITransactionResponse[]>>(baseUrl + '/Transaction/GetTransactions')
    .pipe(map( res => res.data));
}

This is my Model:

export class ITransactionResponse {
    id: number;
    transactionNo?:string;
    valueDate?:Date;
    amount?: number;
}

EDIT:

enter image description here

9
  • i need to change the date format of valueDate?:Date; of array from json Commented Nov 16, 2020 at 11:10
  • i already done in using pipe with map for sinlge object Commented Nov 16, 2020 at 11:12
  • you did already, what else do you want map? Commented Nov 16, 2020 at 11:12
  • @naren in the question i have mention getEditTransactionById.. i need same for getTransactions Commented Nov 16, 2020 at 11:15
  • You already map res to res.data, so why don't you also map every entry of res.data the way you do it in your other service? If you don't know how to iterate over a list maybe start with the basics first. Commented Nov 16, 2020 at 11:20

2 Answers 2

1

Just replace this

getTransactions(): Observable<ITransactionResponse[]> {
    return this.httpClient.get<Result<ITransactionResponse[]>>(baseUrl + '/Transaction/GetTransactions')
    .pipe(map( res => {
           res.data.map(item => {
             item = new Date(item.valueDate)
           })
           return res.data;
     }));
}

To

getTransactions(): Observable<ITransactionResponse[]> {
    return this.httpClient.get<Result<ITransactionResponse[]>>(baseUrl + '/Transaction/GetTransactions')
    .pipe(map( (res : any) => res.data));
}

This is happening because you didn't specify the type of your incoming res and you are trying to access res.data.valueDate so compiler doesn't know the nested fields of res because default type of res is not an object

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks ...But i have to change the format of field valueDate in array
did you try what i mentioned in my answer?
i tried but actally date is showing 2020-11-26T00:00:00 like that.. i need to format dd-mm-yyyy...so i need to convert all dates in array..( i can change to date to string in the model) is there any solution..
your array is coming in which key? in res or in res.data ?
Thnks a lot ...its working ..small changes ..i used moment t convert date to string ...I have replaced date with string..is it possible with date field with format change( i need DD-MM-YYYY)
|
0

After mapping the result to get list of ITransactionResponse, you can map the list of ITransactionResponse, like res.map(x => x.valueDate = new Date(x.valueDate))

1 Comment

i tried nut no luck ..could u please append in my method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.