Angular 17 [vite] Internal server error: Cannot convert undefined or null to object

95 Views Asked by At

After upgrading from Angular 15 to 17 i keep getting this error.

PM [vite] Internal server error: Cannot convert undefined or null to object at Function.getPrototypeOf () at node_modules/whatwg-url/lib/utils.js (/home/dej/src/hi-frontend/node_modules/whatwg-url/lib/utils.js:34:39) at __require2 (/home/dej/src/hi-frontend/.angular/vite-root/hi-frontend/chunk-LO3AJX6B.mjs:54:50) at node_modules/whatwg-url/lib/URL.js (/home/dej/src/hi-frontend/node_modules/whatwg-url/lib/URL.js:4:15) at __require2 (/home/dej/src/hi-frontend/.angular/vite-root/hi-frontend/chunk-LO3AJX6B.mjs:54:50) at node_modules/whatwg-url/webidl2js-wrapper.js (/home/dej/src/hi-frontend/node_modules/whatwg-url/webidl2js-wrapper.js:3:13) at __require2 (/home/dej/src/hi-frontend/.angular/vite-root/hi-frontend/chunk-LO3AJX6B.mjs:54:50) at node_modules/whatwg-url/index.js (/home/dej/src/hi-frontend/node_modules/whatwg-url/index.js:3:34) at __require2 (/home/dej/src/hi-frontend/.angular/vite-root/hi-frontend/chunk-LO3AJX6B.mjs:54:50) at node_modules/jsdom/lib/api.js (/home/dej/src/hi-frontend/node_modules/jsdom/lib/api.js:7:19) (x2)

After narrowing it down I found that its this code that leads to the error:

new User().deserialize(user)

Basically everywhere through out my app where i call the deserialize() function, this error keeps happening.

Example: The get functions in my services all use the deserialize() methods:

getUser(): Observable<User> {
    let url = this.domain + this.prefix + '/user/';
    return this.http.get<User>(url).pipe(
        map(user => new User().deserialize(user))
    );
}

If I for example don't call deserialize() on the response from the server the error goes away. But I need to use deserialize() as I have implemented this on hundreds of places through out the app...

The User class looks like this:

export interface Deserializable {
    deserialize(input: any): this;
}

export class User implements Deserializable {
    id: number;

    deserialize(input: any): this {
        Object.assign(this, input);
        return this;
    }

    getId() {
        return this.id;
    }
}

I tried adding checks for null or undefined in deserialize but its still not working:

deserialize(input: any): this {
    if (input == null || input == undefined)
        return this;

    Object.assign(this, input);
    return this;
}

Any ideas why this is causing the error?

0

There are 0 best solutions below