MongoDB returns nothing when placing the model in another file


MongoDB returns nothing when placing the model in another file



I have a very basic server, which can be run in three ways:



[BLOCK 2 ENABLED] / [BLOCK 1 DISABLED]
Defining interfaces, schema and model in the same file.


[BLOCK 2 ENABLED]


[BLOCK 1 DISABLED]



[BLOCK 1 ENABLED] / [BLOCK 2 DISABLED]
Defining interfaces, schema and model in an external file: ./models/user.ts


[BLOCK 1 ENABLED]


[BLOCK 2 DISABLED]



The content of [BLOCK 2] is exactly the same as the content of file: ./models/user.ts.


[BLOCK 2]



Just in case here you have the repository:



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



Here is the code:



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


[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);



Suppose we have some dummy data on the database:


$ 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" } );



If I run the server with [BLOCK 2] enabled:


[BLOCK 2]


$ ts-node server.ts



I get the following output:


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' } ]



But If I run the server with [BLOCK 1] enabled:


[BLOCK 1]



I just get the following output:


Hello World !!!



without the previous entries.



The only difference I see between both ways is where are defined the interfaces, schema and model.



EDIT 1



I did some experiments:



With way 1 and way 3 mentioned above, if I do:


way 1


way 3


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



then I get the expected output.



But with way 2 mentioned above, If I do:


way 2


$ cd ./server/
$ tsc server.ts



I get the following output:


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.



I think there are conflicts because the directory: ./models/ has its own node_modules directory.


./models/


node_modules



Do you think is there a way to NOT include the dependencies:
{mongoose, @types/mongoose} on the ./server/ directory and just do it on the ./models/ directory to prevent these conflicts?


mongoose


@types/mongoose


./server/


./models/



Any idea on how to make the way 2 work?


way 2



Thanks!





Most likely you have stale .js file compiled from some old variant of user.ts somewhere. Remove all .js files, recompile and try again.
– artem
2 days ago


.js


user.ts


.js





when running $ ts-node server.ts there is no .js file generated. Also, the way 3 above works properly. I think the issue is because defining the interfaces, schema and model in an uncle directory (from server.ts point of view).
– davidesp
2 days ago



$ ts-node server.ts


.js


way 3


server.ts





Then ts-node is somehow importing the wrong file. If contents of the file are the same, it should not matter where it's located.
– artem
2 days ago


ts-node





I added: EDIT 1 on my post above.
– davidesp
2 days ago


EDIT 1





Oh. If models has its own node_modules, then either node_modules should be removed there, or models should be a separate package with it's own package.json which have it's main and types set up properly, it should be compiled separately and used as dependency in server code.
– artem
2 days ago


models


node_modules


node_modules


models


package.json


main


types









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV