0

We have services.ts . Which needs 2 arguments

UPDATED seems like i was looking the error wrong. this method was causing the console error

private checkLink(row, options) {
        if (options.type ==='link' || options.type ==='custom-method') {
            if (options.type === 'link' || (options.type === 'custom-method' && (row.user_id === this.userData.id) || this.authService.permission('view_subject_data') || this.userData.id === row.section.user_id || this.userData.id === 1)) {
                if (options.permission) {
                    if (this.authService.permission(options.permission)) {
                        this.router.navigate([`${options.link}/${row[options.linkKey]}`]);
                    }
                } else {
                    this.router.navigate([`${options.link}/${row[options.linkKey]}`]);
                }
            }
        }
    }

Im calling it to my html table which is used by many components.

<span [innerHtml]="datatableService.setColumnData(row[column.prop], column.options, row)"

Here's the data that im passing to setColumnData

columns = [
        {
            name: 'User',
            prop: 'last_name',
            options: {
                type: 'piped',
                pipes: ['displayFullName']
            },
        },
        { 
            name: 'Action',
            prop: 'description'
        },
        {
            name: 'Time',
            prop: 'created_at',
            options: {
                type: 'piped',
                pipes: ['formatDateTime']
            },
        },

    ];

Which gives me type undefined but code works. I just need to get rid of console.log error.

enter image description here

I know i just need to supply the needed arguments like this. And it works no errors.

{
    name: 'Action',
    prop: 'description',
    options: ''
},

But the problem is i need to change all the similar columns variable all across the project. Is there anyway easier or clean way to do this?

4
  • One cannot just disable undefined. Type your objects, then the compiler will force you in the right direction. As a bonus, you won't get this kind of runtime errors. Commented Aug 11, 2022 at 5:58
  • Thanks for taking the time to answer. Yes bro. But when i add options to my columns array. The error doesn't show. Like in the last part in my post. Commented Aug 11, 2022 at 6:01
  • I understand that, but to get rid of the error in all of the places, you need to do your if check everywhere. How come you did not get this error until now? You said you have this in a lot of places. Commented Aug 11, 2022 at 6:04
  • Im not the one who code this. Maybe my past senior devs. Im just fixing the code now cause most of them left. I see thanks for the hint bro. Commented Aug 11, 2022 at 6:06

1 Answer 1

1

Have you tried calling the service & storing the value in your component.ts file through ngAfterViewInit instead of calling it directly in the component.html file, The values are initiated after the view is rendered so it should throw undefined, since the value is not initiated

public tableData:yourType = []

ngAfterViewInit():void{
  this.tableData = this.datatableService.setColumnData(this.row[this.column.prop], this.column.options, this.row)
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer bro. Not yet bro. thinking of, if check like octavian suggested
okay bro i would recommend using the ts file for handling all the data coming from a service, since we don't have much control over the data in the html file :)

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.