Reputation: 360
So I have a Node /w Typescript REST API, I have signup method which creates a user and responses with the created users firstName, lastName, email.
The problem is I am having this typescript error that says "Type 'Document' is missing the following properties from type 'SavedUser': firstName, lastName, email".
I believe its something with adding mongoose.Document type in my SavedUser Interface, i am not sure tho, thanks for the help!
Sample Code:
interface SavedUser {
firstName: string
lastName: string
email: string
}
...
public async signUp(req: Request, res: Response): Promise<void | Response> {
const salt: string = await bcrypt.genSalt(10)
const hashedPassword: string = await bcrypt.hash(req.body.password, salt)
const user = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: hashedPassword
})
try {
const { firstName, lastName, email }: SavedUser = await user.save()
return res.status(200).send({
firstName,
lastName,
email
})
} catch (err) {
return res.status(400).send(err)
}
}
Upvotes: 6
Views: 33918
Reputation: 360
Thanks for answering I solved it! I mixed dijkstra and phw's answer and came up with this:
In my User Model
//user.ts
import { Schema, model, Document } from 'mongoose'
const userSchema = new Schema({
firstName: {
type: String,
required: true,
min: 2,
max: 255
},
lastName: {
type: String,
required: true,
min: 2,
max: 255
},
email: {
type: String,
required: true,
unique: true,
min: 6,
max: 255
}
})
export interface SavedUserDocument extends Document {
firstName: string;
lastName: string;
}
export const userSchemaModel = model<SavedUserDocument>('User', userSchema)
now on my User Controller:
//user.ts
import { userSchemaModel, SavedUserDocument } from '../../models/user/user'
...
public async signUp(req: Request, res: Response): Promise<void | Response> {
const user = new userSchemaModel({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email
})
try {
const { firstName, lastName }: SavedUserDocument = await user.save()
return res.status(200).send({
firstName,
lastName,
message: 'User created'
})
} catch (err) {
return res.status(400).send(err)
}
}
...
I do have a question;
If I removed the <SavedUserDocument>
in model<SavedUserDocument>('User', userSchema)
I would still receive the error, can i have a good explanation of that? i'm still relatively new with Typescript and would be great to get an explanation, Thank you very much!
Upvotes: 4
Reputation: 128
Mongoose returns more on .save()
then you are currently specifying with the SavedUser interface.
The easiest way of getting all the types from Mongoose, is by using the exported Document
and extending your interface.
import { Document } from 'mongoose';
export interface SavedUser extends Document {
email: string;
firstName: string;
lastName: string;
}
Upvotes: 5
Reputation: 2679
I do not know where the problem exactly but this is how I create mongoose schema using TypeScript and it works for me.
import * as mongoose from 'mongoose';
interface SavedUserDocument extends mongoose.Document {
firstName: string;
lastName: string;
email: string;
}
const SavedUserSchema = new mongoose.Schema({...});
export const SavedUser = mongoose.model<SavedUserDocument>('saveduser', SavedUserSchema);
Hope it works for you as well.
Upvotes: 8