1

My code looked like this but now I put it into a Typescript class and it gives me an error saying that "can't read property role of undefined".

    this.$http({
        method: 'POST',
        url: '/Token',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: 'grant_type=password&username=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password),
    })
        .success(function (data, status, headers, cfg) {
            data.roles.split(':').forEach(function (v) {
                this.data.role['is' + v] = true;
                v == 'Test'
        })

Do I have to code my function differently with Typescript ? I have two function in the success(), can I replace this with => ?

1

1 Answer 1

1

This in this case this means instance of the class where this code runs. You can definitely convert the functions to arrow functions (=>) and omit this. The code would look like this then:

this.$http({
    method: 'POST',
    url: '/Token',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: 'grant_type=password&username=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password),
})
.success((data, status, headers, cfg) => {
    data.roles
        .split(':')
        .forEach((v) => {
            data.role['is' + v] = true;
            v == 'Test'
        })
});
Sign up to request clarification or add additional context in comments.

Comments

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.