In my Node.js TypeScript project, the structure is as follows:
https://i.stack.imgur.com/YgFjd.png
The crucial part of the structure lies in mongoModels. I have 2 models where each Category model is connected and contains field category.expertUserIds which holds an array of user._id.
user.ts:
const { Schema, model } = require("mongoose");
const userSchema = new Schema({
username: {
type: String,
require: true,
},
password: {
type: String,
require: true,
},
});
module.exports = model("User", userSchema);
category.ts:
const { Schema, model } = require("mongoose");
const categorySchema = new Schema({
name: {
type: String,
require: true,
},
description: {
type: String,
},
expertUserIds: [
{
type: Schema.Types.ObjectId,
ref: "User",
},
],
});
module.exports = model("Category", categorySchema);
I have multiple projects based on the same concept created as regular .js files, but when using TypeScript, an error occurs:
mongoModels/category.ts:1:17 - error TS2451: Cannot redeclare block-scoped variable 'model'.
1 const { Schema, model } = require("mongoose"); ~~~~~
This issue also applies to Schema in both files. TypeScript considers that I have already declared:
const { Schema, model } = require("mongoose");
once and it cannot be done again for another file. How can this error be resolved since Schema and model are needed in different files?