简体   繁体   English

类型“文档”缺少类型中的以下属性

[英]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.所以我有一个 Node /w Typescript REST API,我有注册方法,它创建一个用户并使用创建的用户 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".问题是我有这个打字稿错误,上面写着“类型‘文档’缺少来自‘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!我相信它在我的 SavedUser 界面中添加 mongoose.Document 类型,我不确定,谢谢你的帮助!

Error ScreenShot:错误截图: 在此处输入图片说明

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)
        }
    }

I do not know where the problem exactly but this is how I create mongoose schema using TypeScript and it works for me.我不知道问题究竟出在哪里,但这就是我使用 TypeScript 创建猫鼬模式的方式,它对我有用。

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.希望它也适用于您。

Mongoose returns more on .save() then you are currently specifying with the SavedUser interface. Mongoose 在.save()上返回更多,然后您当前使用 SavedUser 接口指定。

The easiest way of getting all the types from Mongoose, is by using the exported Document and extending your interface.从 Mongoose 获取所有类型的最简单方法是使用导出的Document并扩展您的接口。

import { Document } from 'mongoose';

export interface SavedUser extends Document {
  email: string;
  firstName: string;
  lastName: string;
}

Thanks for answering I solved it!谢谢回答我解决了! I mixed dijkstra and phw 's answer and came up with this:我混合了dijkstraphw的答案,然后想出了这个:

In my User Model在我的用户模型中

//user.ts //用户.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 //用户.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?如果我删除了<SavedUserDocument>model<SavedUserDocument>('User', userSchema)我仍然会收到错误,我可以有一个很好的解释? i'm still relatively new with Typescript and would be great to get an explanation, Thank you very much!我对 Typescript 还是比较新的,如果能得到解释会很高兴,非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“{}”缺少类型“IResponseConfig”中的以下属性 - Type '{}' is missing the following properties from type 'IResponseConfig' 错误:类型“{}”缺少类型中的以下属性 - Error: Type '{}' is missing the following properties from type 类型缺少类型的以下属性? - Type is missing the following properties from type? “EventTarget”类型缺少以下属性 - Missing the following properties from type 'EventTarget' Typescript Angular - 接收类型缺少以下类型的属性 - Typescript Angular - Recieving Type is missing following properties from type TS 错误:“事件”类型缺少“键盘事件”类型中的以下属性 - TS error: Type 'event' is missing the following properties from type 'keyboardevent' 输入“分页”<UserEntity> &#39; 缺少来自类型 &#39;UserEntity&#39; 的以下属性: - Type 'Pagination<UserEntity>' is missing the following properties from type 'UserEntity': 类型&#39;CategoryModel []&#39;缺少类型&#39;GenericModel的以下属性<CategoryModel[]> - Type 'CategoryModel[]' is missing the following properties from type 'GenericModel<CategoryModel[]> Angular - 类型“{}”缺少类型“any[]”的以下属性 - Angular - Type '{}' is missing the following properties from type 'any[]' 类型 A 缺少类型 B 的以下属性:a、b - Type A is missing the following properties from type B: a, b
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM