admin管理员组

文章数量:1315792

I have created mongoose schema for user collection now i am trying to import in the controller it shows error no exported member 'User'. Did you mean 'IUser'? how to fix this issue ?

user.model.ts

import mongoose, { Schema } from 'mongoose';

export interface IUser extends mongoose.Document {
  firstName: string,
  lastName: string,
  userName: string 
  password: string 
}
 const UserSchema: Schema = new Schema({
    firstName: {
        type: String,
        required: true
      },
    lastName: {
        type: String,
        required: true
      },
   userName: {
    type: String,
    required: true
  },
   password: {
    type: String,
    required: true
  }
});

export default mongoose.model('User', UserSchema);

user.controller.ts

import { Request, Response } from 'express';
import { User } from './user.model'

I have created mongoose schema for user collection now i am trying to import in the controller it shows error no exported member 'User'. Did you mean 'IUser'? how to fix this issue ?

user.model.ts

import mongoose, { Schema } from 'mongoose';

export interface IUser extends mongoose.Document {
  firstName: string,
  lastName: string,
  userName: string 
  password: string 
}
 const UserSchema: Schema = new Schema({
    firstName: {
        type: String,
        required: true
      },
    lastName: {
        type: String,
        required: true
      },
   userName: {
    type: String,
    required: true
  },
   password: {
    type: String,
    required: true
  }
});

export default mongoose.model('User', UserSchema);

user.controller.ts

import { Request, Response } from 'express';
import { User } from './user.model'
Share Improve this question asked Jul 8, 2020 at 17:01 hussainhussain 7,12321 gold badges87 silver badges164 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

For Typescript:

export interface IUser extends mongoose.Document {
  name: string; 
  somethingElse?: number; 
};

export const UserSchema = new mongoose.Schema({
  name: {type:String, required: true},
  somethingElse: Number,
});

const User = mongoose.model<IUser>('User', UserSchema);
export default User;

When saying export default ...;

when you want to import it just type:

import User from "./user.model";

Notice: User can be anything for example MyUserSchema or anything

本文标签: javascriptHow to export mongoose schema using typescriptStack Overflow