javascript - 如何使用 typescript 导出 Mongoose 模式?

标签 javascript node.js typescript mongoose

我已经为用户集合创建了 mongoose 架构,现在我正在尝试导入 Controller ,它显示错误 no exported member 'User'。您是说“IUser”吗?如何解决这个问题?

用户.模型.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);

用户.controller.ts

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

最佳答案

对于 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;

关于javascript - 如何使用 typescript 导出 Mongoose 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62799889/

相关文章:

node.js - Angular 7 中 ActiveXObject 的问题

javascript - Google map 上的文本框值更改更新标记

javascript - 如何将 RFC822 消息体 body 存储为 String?

javascript - Angular : Select combination from a list

node.js - 强制 nodemailer 仅使用推荐的密码执行 TLS 1.2

javascript - 将 PDF 二进制数据编码为 base64 不适用于 NodeJS

javascript - Typescript 默认函数参数

typescript - aurora serverless Data Api 是否有任何模拟(或本地)服务?

javascript - 在 Meteor 中导入 ES6 npm 模块时出现语法错误

php - 查看已经过去了多少天