I spent 5 hours trying to solve this problem but couldn't figure out what's wrong
in the file -routes/users.ts,
The line ""targetUser.token = token" is working, so console.log(targetUser) shows the updated user data.
However, targetUser.save() is not updating this data in MONGO DB... why is that?
-User.ts-
import mongoose, { Document, Schema } from "mongoose";
import bcrypt from "bcrypt";
const saltRounds = 10;
const userSchema = new Schema({
name: {
type: String,
maxlength: 50,
},
email: {
type: String,
trim: true,
unique: 1,
},
password: {
type: String,
minlength: 5,
},
lastname: {
type: String,
maxlength: 50,
},
// Rest of the schema properties...
export interface UserBaseDocumentType extends Document {
name: string;
email: string;
password: string;
lastname: string;
role: number;
art: [];
history: [];
image: string;
token: string;
tokenExp: string;
comparePassword(plainPassword: string): Promise<boolean>;
}
// Code for pre-save middleware and methods...
///////////////export this model////////////////////////////
export default mongoose.model<UserBaseDocumentType>("User", userSchema);
///////////////////////////////////////////
-index.ts-
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import mongoose from "mongoose";
const mongoDBAccessKey = require("./config/key");
import userRouter from "./routes/users";
// App initialization and setting up middlewares...
// MongoDB connection setup...
// Routes setup...
// Port configuration and server start...
It seems like TypeScript doesn't recognize mongoose's Document type, causing issues with the save() method. Any help would be greatly appreciated.