将模型放在另一个文件中时,MongoDB不返回任何内容

时间:2018-06-28 23:29:09

标签: node.js mongodb typescript express

我有一个非常基本的服务器,可以通过三种方式运行:

  1. [BLOCK 2 ENABLED] / [BLOCK 1 DISABLED]
    在同一文件中定义接口,架构和模型。

  2. [BLOCK 1 ENABLED] / [BLOCK 2 DISABLED]
    在外部文件中定义接口,架构和模型: ./ models / user.ts

[BLOCK 2]的内容与文件的内容完全相同: ./ models / user.ts

  
      
  1. 另一方面,如果我将文件: user.ts 移至:
       ./ server / models / user.ts
      然后代码可以正常工作,但是出于某些特定原因,我需要在以下位置进行访问:
       ./ models / user.ts
  2.   

以防万一,这里有存储库:

https://github.com/napolev/mongodb-experiments

代码如下:

./ server / server.ts [BLOCK 2 ENABLED]

import * as mongoose from 'mongoose';
import * as colors from 'colors';
/*/
// BLOCK 1:
import { User, IUserDocument } from '../models/user';
/*/
// BLOCK 2:
import { Document, Schema, Model, model} from "mongoose";
export interface IUser {
    username:   string,
    password:   string,
    email:      string,
    company:    string,
}
export interface IUserDocument extends IUser, Document {

}
export var UserSchema: Schema = new Schema({
    username:   String,
    password:   String,
    email:      String,
    company:    String,
});
export const User: Model<IUserDocument> = model<IUserDocument>("User", UserSchema);
//*/

mongoose.connect('mongodb://localhost:9100/mongodb-experiments');

User.find({}, function(err, users: IUserDocument[]){
    console.log(users);
});

let message = colors.green('Hello') + ' '  + colors.red.bgYellow.underline('World') + ' ' +  colors.blue.bgRed('!!!');

console.log(message);

// To work with the Mongo Shell:
// $ cd /path/to/mongodb/bin"
// $ mongo --port 9100
// use mongodb-experiments
// db.users.insert( { username: "bill.gates", password: "windows", email: "billgates@microsoft.com", company: "Microsoft" } )
// db.users.insert( { username: "steve.jobs", password: "mac", email: "stevejobs@apple.com", company: "Apple" } );
// db.getCollection("users").find()

./ models / user.ts

import { Document, Schema, Model, model} from "mongoose";

export interface IUser {
    username:   string,
    password:   string,
    email:      string,
    company:    string,
}
export interface IUserDocument extends IUser, Document {

}
export var UserSchema: Schema = new Schema({
    username:   String,
    password:   String,
    email:      String,
    company:    String,
});

export const User: Model<IUserDocument> = model<IUserDocument>("User", UserSchema);

假设我们在数据库中有一些虚拟数据:

$ cd /path/to/mongodb/bin"
$ mongo --port 9100
use mongodb-experiments
db.users.insert( { username: "bill.gates", password: "windows", email: "billgates@microsoft.com", company: "Microsoft" } )
db.users.insert( { username: "steve.jobs", password: "mac", email: "stevejobs@apple.com", company: "Apple" } );

如果我在启用[BLOCK 2]的情况下运行服务器:

$ ts-node server.ts

我得到以下输出:

Hello World !!!
[ { _id: 5b3561bbcde3135b908cb51b,
    username: 'bill.gates',
    password: 'windows',
    email: 'billgates@microsoft.com',
    company: 'Microsoft' },
  { _id: 5b3561c1cde3135b908cb51c,
    username: 'steve.jobs',
    password: 'mac',
    email: 'stevejobs@apple.com',
    company: 'Apple' } ]

但是如果我在启用[BLOCK 1]的情况下运行服务器:

我只得到以下输出:

Hello World !!!

没有以前的条目。

两种方法之间的唯一区别是接口,架构和模型的定义位置。

编辑1

我做了一些实验:

如果有的话,使用上面提到的way 1way 3

$ cd ./server/
$ tsc server.ts
$ node server.js

然后我得到预期的输出

但是如果有上述way 2,如果我这样做:

$ cd ./server/
$ tsc server.ts

我得到以下输出:

node_modules/@types/mongoose/index.d.ts(61,9): error TS2300: Duplicate identifier 'NativeBuffer'.
node_modules/@types/mongoose/index.d.ts(62,9): error TS2300: Duplicate identifier 'NativeDate'.
node_modules/@types/mongoose/index.d.ts(63,9): error TS2300: Duplicate identifier 'NativeError'.
node_modules/@types/mongoose/index.d.ts(71,14): error TS2300: Duplicate identifier 'Mongoose'.
node_modules/@types/mongoose/index.d.ts(72,8): error TS2300: Duplicate identifier 'Mongoose'.
node_modules/@types/mongoose/index.d.ts(156,9): error TS2300: Duplicate identifier 'CastError'.
node_modules/@types/mongoose/index.d.ts(177,18): error TS2300: Duplicate identifier 'ConnectionBase'.
node_modules/@types/mongoose/index.d.ts(447,9): error TS2300: Duplicate identifier 'Connection'.
node_modules/@types/mongoose/index.d.ts(463,9): error TS2300: Duplicate identifier 'ValidationError'.
node_modules/@types/mongoose/index.d.ts(472,9): error TS2300: Duplicate identifier 'Error'.
node_modules/@types/mongoose/index.d.ts(562,9): error TS2300: Duplicate identifier 'VirtualType'.
node_modules/@types/mongoose/index.d.ts(579,9): error TS2300: Duplicate identifier 'Schema'.
node_modules/@types/mongoose/index.d.ts(888,5): error TS2374: Duplicate string index signature.
node_modules/@types/mongoose/index.d.ts(1018,5): error TS2374: Duplicate string index signature.
node_modules/@types/mongoose/index.d.ts(1072,13): error TS2300: Duplicate identifier 'MongooseDocument'.
node_modules/@types/mongoose/index.d.ts(1073,9): error TS2300: Duplicate identifier 'MongooseDocument'.
node_modules/@types/mongoose/index.d.ts(1275,11): error TS2300: Duplicate identifier 'Subdocument'.
node_modules/@types/mongoose/index.d.ts(1291,11): error TS2300: Duplicate identifier 'Array'.
node_modules/@types/mongoose/index.d.ts(1400,11): error TS2300: Duplicate identifier 'DocumentArray'.
node_modules/@types/mongoose/index.d.ts(1429,11): error TS2300: Duplicate identifier 'Buffer'.
node_modules/@types/mongoose/index.d.ts(1461,10): error TS2300: Duplicate identifier 'ObjectIdConstructor'.
node_modules/@types/mongoose/index.d.ts(1469,11): error TS2300: Duplicate identifier 'Decimal128'.
node_modules/@types/mongoose/index.d.ts(1475,11): error TS2300: Duplicate identifier 'Embedded'.
node_modules/@types/mongoose/index.d.ts(1515,9): error TS2300: Duplicate identifier 'Query'.
node_modules/@types/mongoose/index.d.ts(1516,9): error TS2300: Duplicate identifier 'DocumentQuery'.
node_modules/@types/mongoose/index.d.ts(1948,9): error TS2300: Duplicate identifier 'mquery'.
node_modules/@types/mongoose/index.d.ts(2000,13): error TS2300: Duplicate identifier 'Schema'.
node_modules/@types/mongoose/index.d.ts(2248,9): error TS2300: Duplicate identifier 'Aggregate'.
node_modules/@types/mongoose/index.d.ts(2411,9): error TS2300: Duplicate identifier 'SchemaType'.
node_modules/@types/mongoose/index.d.ts(2864,5): error TS2374: Duplicate string index signature.
node_modules/@types/mongoose/index.d.ts(2966,5): error TS2374: Duplicate string index signature.

我认为存在冲突,因为目录./models/有自己的node_modules目录。

您认为有没有办法包含依赖项:
{mongoose@types/mongoose}在./server/目录上,只是在./models/目录上这样做以防止这些冲突?

关于如何使way 2工作的任何想法?

谢谢!

0 个答案:

没有答案