简体   繁体   English

使用打字稿的Mongoose输入问题

[英]Mongoose typing issue with typescript

I am building an app using mongoose and typescript. 我正在使用mongoose和typescript构建一个应用程序。 Here is a simple model I have made: 这是我制作的简单模型:

import * as callbackMongoose from 'mongoose';
var mongoose = callbackMongoose;
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;

var userSchema = new Schema({
    username: String,
    email: String,
    hash: String
});

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

It works well but I need to cast each document to any before accessing properties. 它运行良好但我需要在访问属性之前将每个文档转换为任何文档。 I read a guide that said I could do this: 我读了一个说我可以这样做的指南:

interface IUser extends mongoose.Document {
  username: String;
  email: String;
  hash: String;
}

export default mongoose.model<IUser>('User', userSchema);

My problem is that the type mongoose doesn't seem to have the property Document . 我的问题是类型mongoose似乎没有属性Document It also doesn't have the property ObjectId . 它也没有ObjectId属性。 When I cast mongoose to any and use these members it works just fine. 当我把猫鼬投到任何人并使用这些成员时它工作得很好。 It seems to be a typing issue. 这似乎是打字问题。

I installed the mongoose typing like so: 我像这样安装了猫鼬类型:

npm install @types/mongoose --save

The typings do work for Schema and they are good for all of the other libraries I use. 这些类型适用于Schema,它们适用于我使用的所有其他库。 Is something wrong with these type definitions? 这些类型定义有问题吗? Am I doing something wrong? 难道我做错了什么?

For TypeScript@2.0 I think you may use 对于TypeScript@2.0,我想你可以使用

npm install @types/mongoose --save

instead of: 代替:

npm install @typings/mongoose --save

This is full example: 这是完整的例子:

Database.ts Database.ts

import mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect('mongodb://admin:123456@ds149437.mlab.com:49437/samples');

export { mongoose };

UserData.ts UserData.ts

import { mongoose } from './../../Services/Database';

export interface UserData {
    is_temporary: boolean;
    is_verified: boolean;
    status: boolean;
    username: string;
}

export interface IUserData extends UserData, mongoose.Document, mongoose.PassportLocalDocument { };

UserModel.ts UserModel.ts

import { IUserData } from './UserData';
import { mongoose } from './../../Services/Database';

import * as passportLocalMongoose from 'passport-local-mongoose';
import Schema = mongoose.Schema;


const UserSchema = new Schema({
  username: { type: String, required: true },
  password: String,
  status: { type: Boolean, required: true },
  is_verified: { type: Boolean, required: true },
  is_temporary: { type: Boolean, required: true }
});

UserSchema.plugin(passportLocalMongoose);

var UserModel;
try {
  // Throws an error if 'Name' hasn't been registered
  UserModel = mongoose.model('User')
} catch (e) {
  UserModel = mongoose.model<IUserData>('User', UserSchema);
}

export = UserModel;

I also full project example using typescript, node.js, mongoose & passport.js right here: https://github.com/thanhtruong0315/typescript-express-passportjs 我也在这里使用typescript,node.js,mongoose和passport.js的完整项目示例: https//github.com/thanhtruong0315/typescript-express-passportjs

Good luck. 祝好运。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM