Im trying to get a response from my '/users/:uid' endpoint, where it should return data of the specific user if the user is in the database, but it does not work. I get this error: The error
I have a userController file, where i have implemented the function for finding the user based on id in the URL and the userController file is seen in the below:
import { Request, Response } from "express";
import mongoose from "mongoose";
import { schema } from "../models/User";
import { User } from "../models/User";
import {ROUNDS, SALT_LENGTH, KEY_LENGTH, ITERATIONS, DIGEST, pbkdf2, randomBytes} from
"../../authCrypto"
import { join } from 'path'
const X5U = 'http://localhost:3000/auth-rsa256.key.pub';
const PATH_PRIVATE_KEY = join(__dirname, '..', '..', 'auth-rsa.key');
const usersConnection = mongoose.createConnection('mongodb://localhost:27017/users')
const UserModel = usersConnection.model('User', schema)
const listUsers = async (req: Request, res: Response) => {
let result = await UserModel.find({}, ).lean().exec();
res.json(result);
};
const getUser = async (req: Request, res: Response) => {
const {uid} = req.params
let result = await UserModel.find({_id: uid}).lean().exec();
if(!result){
res.json({
"message": "The user is not found"
});
}
res.json(result);
}
module.exports = {
listUsers,
getUser
};
And this is my User model:
import { Schema } from 'mongoose';
export enum Role {
manager = "Manager",
clerk = "Clerk",
guest = "Guest"
}
export interface User{
firstName: string;
middleName: string;
lastName: string;
email: string;
role: Role;
password: string;
salt: string;
}
export const schema = new Schema<User>({
firstName: { type: String, required: true },
middleName: { type: String, required: true },
lastName: { type: String, required: true },
email: { type: String, required: true },
role: { type: String, required: true },
password: { type: String, required: true },
salt: { type: String, required: true},
});
And this is my userRouter:
import { Router } from "express";
const userController = require('../controller/userController')
const userRouter = Router();
userRouter.get('/users', userController.listUsers);
userRouter.get('/users/:uid', userController.getUser);
module.exports = userRouter;
And this is my index file, which is the startfile of the application:
const express = require("express");
const app = express();
const port = 3000;
app.use('/', require("./router/userRouter"));
app.use('/rooms', require('./router/roomRouter'));
app.use('/reservations', require('./router/reservationRouter'));
app.listen(port, () => {
return console.log(`Express is listening at http://localhost:${port}`);
});