After calling the function to retrieve data from MongoDB, an undefined error occurs. It is suspected that converting the function to an async/await function may resolve this issue. However, there is uncertainty on how to make this conversion without disrupting the existing functions and their chains. Is there a straightforward way to switch this to async/await?
studentRoute.route('/read-student/:id').get((req, res) => {
Student.findById(req.params.id, (error, data) => {
if (error) {
return next(error)
} else {
res.json(data)
}
})
})
The angular front end features the following service:
// Get student
GetStudent(id): Observable<any> {
let API_URL = `${this.endpoint}/read-student/${id}`;
return this.http.get(API_URL, { headers: this.headers })
.pipe(
map((res: Response) => {
return res || {}
}),
catchError(this.errorMgmt)
)
}
Within the angular component ts file contract.component.ts:
import { Component, ViewChild, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ApiService } from './../../shared/api.service';
import { Student } from './../../shared/student';
import { Web3Service } from './../../shared/web3.service';
import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-contract',
templateUrl: './contract.component.html',
styleUrls: ['./contract.component.css']
})
export class ContractComponent implements OnInit {
StudentData: any = [];
dataSource: MatTableDataSource<Student>;
@ViewChild(MatPaginator) paginator: MatPaginator;
displayedColumns: string[] = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l'
];
constructor(
private router: Router,
private actRoute: ActivatedRoute,
private studentApi: ApiService
) {
var id = this.actRoute.snapshot.paramMap.get('id');
this.studentApi.GetStudent(id).subscribe(data => {
this.StudentData = data;
this.dataSource = new MatTableDataSource<Student>(this.StudentData);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
}, 0);
})
}
ngOnInit(): void {
}
}
An undefined error arises in the corresponding contract.component.html file when receiving data from MongoDB using findbyid()
Reference tutorial: