Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Document' is missing the following properties from type

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!

Error ScreenShot: enter image description here

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)
        }
    }
like image 952
frost kazuma Avatar asked Dec 17 '19 08:12

frost kazuma


People also ask

Is missing the following properties from type?

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.

Is missing the following properties from type element class?

js error "Type is missing the following properties from type" occurs when we don't pass any props to a component, or don't pass it all of the props it requires. To solve the error, make sure to provide all of the props that are required in the component.

What type has no properties in common?

The error "Type 'X' has no properties with type 'Y'" occurs when we try to assign anything to a weak type when there's no overlap in properties. To solve the error, declare any overlapping properties if they exist or use a type assertion. Here is an example of how the error occurs.


3 Answers

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.

like image 93
Shihab Avatar answered Oct 12 '22 08:10

Shihab


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;
}
like image 43
phw Avatar answered Oct 12 '22 09:10

phw


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!

like image 38
frost kazuma Avatar answered Oct 12 '22 09:10

frost kazuma