Angular http request returns status and body undefined
I recently deployed an nx monorepo (angular+nestjs) to the server. I am using MySQL as a database. Everything works fine. I am able to log in and log out and add users to the database. The problem is I am only able to access certain data from the database and the rest of API calls display status and body undefined. Sorry if it is not a good explanation of my issue. I am trying to fix it for days and could not find any solution to that. I recreated the whole database. Even rebuilt the sever to default settings and tried to modify the nestjs codes but the result was the same (status and body undefined). On the other hand, it is working completely fine on the local server. I don't know whether the error belongs to angular, nestjs, database, or maybe the server configuration is wrong!!!
one difference between 200 and 500 status in response headers I notice is, in 200 requests it is stated content-encoding: br while in the others that I get 500 error, there is no such line. Does that help?
I found this code as part of a simple express api that parse data as json.
app.get('/api/cars', (req, res) => {
const payload = {id: id, cars: ['BMW', 'Mercedes-Benz', 'Lamborghini',
'Ferrari', 'Maserati']};
res.json(payload);
});I have not added any .json() code somewhere in nestjs. Do I need to add that to work properly? Basically, the main issue is it was working completely fine on localhost but after deployment, some nestjs api route returns data properly some returns 500 and as status and body error undefined. I remember the first time it was returning [object object] now it is just returning undefined.
I would appreciate it if someone helps me fix it. Thank you in advance.
console error:
main.0256ee13eb46fbfb6d05.js:1
ERROR Http failure response for https://symbiota2.org/api/emails?page=1: 500 OK
zu {
headers: xu,
status: 500,
statusText: 'OK',
url: 'https://symbiota2.org/api/users?page=1',
ok: false,
…}
error: {
statusCode: 500,
message: 'Internal server error'
}
headers: xu {
normalizedNames: Map(0),
lazyUpdate: null,
lazyInit: ƒ
}
message: "Http failure response for https://symbiota2.org/api/users?page=1: 500 OK"
name: "HttpErrorResponse"
ok: false
status: 500
statusText: "OK"
url: "https://symbiota2.org/api/users?page=1"
[[Prototype]]: Lu
main.0256ee13eb46fbfb6d05.js:1 Backend returned code undefined, body was: undefined
polyfills.c7e27d83c905386bcf96.js:1 GET https://symbiota2.org/api/users?page=1 500
main.0256ee13eb46fbfb6d05.js:1 error is intercept
main.0256ee13eb46fbfb6d05.js:1 zu {headers: xu, status: 500, statusText: 'OK', url: 'https://symbiota2.org/api/users?page=1', ok: false, …}
main.0256ee13eb46fbfb6d05.js:1 Backend returned code undefined, body was: undefinedusers.component.ts
load(): void {
this.userService.all(this.page).subscribe(
(res: any | null | undefined) => {
this.users = res;
console.log('this is all res', res);
this.last_page = res.meta.last_page;
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
// we never seem to get here
console.log('An error occurred:', err.error.message);
} else {
// no network connection, HTTP404, HTTP500, HTTP200 & invalid JSON
console.log(
`Backend returned status ${err.status}, body is: ${err.error}`
);
}
}
);
}
ngOnInit(): void {
this.load();
} rest.service.ts (angular)
all(page?: number): Observable<any> {
let url = this.endpoint;
if (page) {
url += `?page=${page}`;
}
return this.http.get<any>('/api/users');
}user.service.ts (nestjs)
async paginate(page = 1, relations = []): Promise<PaginatedResult> {
const { data, meta } = await super.paginate(page, relations);
return {
data: data.map((user) => {
const { password, ...data } = user;
return data;
}),
meta,
}
}
user.controller.ts (nestjs)
@Post()
@HasPermission('users')
async createUser(@Body() body: UserCreateDto): Promise<User> {
const salt = await bcrypt.genSalt();
const password = await bcrypt.hash('A123!@#', salt);
const { roleId = 3, ...data } = body;
return this.userService.create({
...data,
password,
role: { id: roleId },
});
}abstract.service.ts (nestjs)
async paginate(page = 1, relations = []): Promise<PaginatedResult> {
try {
const take = 10;
const [data, total] = await this.repository.findAndCount({
take,
skip: (page - 1) * take,
relations,
});
return {
data: data,
meta: {
total,
take,
page,
last_page: Math.ceil(total / take),
},
};
} catch (error) {
if (error.code === '23505') {
// duplicate user
throw new ConflictException('This record already exists');
} else {
throw new InternalServerErrorException('this is an unknown error inside abstact.service.ts in common libs');
}
console.log(error.code);
}
}
}paginated-result.interface.ts (nestjs)
/* eslint-disable @typescript-eslint/no-explicit-any */
export class PaginatedResult {
data: any[];
meta: {
total: number;
take: number;
page: number;
last_page: number;
};
}from Recent Questions - Stack Overflow https://ift.tt/3jXaqA6
https://ift.tt/eA8V8J
Comments
Post a Comment