Encountering an issue while attempting to locate my user model: Object is possibly 'null'. I would like to find a solution that does not involve suppressing TypeScript's strict rule.
const { email, password } = req.body;
const user = await User.findOne({ email:email });
if (!user) {
}
/// This line causes the error
const passwordMatch = await bcrypt.compare(password, user.password);
////User Interface
import { Document } from 'mongoose';
interface User extends Document {
email: string;
password: string;
username: string;
}
export default User;
////User Schema
import mongoose, { Document, Schema, Model, model } from 'mongoose';
import User from '../interfaces/User';
const UserSchema: Schema = new Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
username: { type: String, required: true },
});
export default mongoose.model<User>('User', UserSchema);