How to set up Typegoose
up vote
1
down vote
favorite
How do I setup Typegoose for a NodeJs REST API and typescript?
I always get weird error messages like MongoParseError: Incomplete key value pair for option
or I do not receive any data, although there are some.
Can someone provide a complete example ?
node.js mongodb typescript mongoose
add a comment |
up vote
1
down vote
favorite
How do I setup Typegoose for a NodeJs REST API and typescript?
I always get weird error messages like MongoParseError: Incomplete key value pair for option
or I do not receive any data, although there are some.
Can someone provide a complete example ?
node.js mongodb typescript mongoose
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How do I setup Typegoose for a NodeJs REST API and typescript?
I always get weird error messages like MongoParseError: Incomplete key value pair for option
or I do not receive any data, although there are some.
Can someone provide a complete example ?
node.js mongodb typescript mongoose
How do I setup Typegoose for a NodeJs REST API and typescript?
I always get weird error messages like MongoParseError: Incomplete key value pair for option
or I do not receive any data, although there are some.
Can someone provide a complete example ?
node.js mongodb typescript mongoose
node.js mongodb typescript mongoose
asked Nov 11 at 1:34
Frederic Reisenhauer
155
155
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
If you want to programm an API under the following conditions, you can use the provided minimal example below:
Conditions:
NodeJS
Typescript
MongoDB (locally)
Example:
If you are working with typescript, a project structure, similar to this is recommended:
├── dist
| ├── (your compiled JS)
├── src
| ├── models
| | ├── user.model.ts
| ├── user-repository.ts
| ├── app.ts
| ├── index.ts
├── test
| ├── (your tests)
Furthermore, it's required to install the following packages
npm install --save typescript mongoose express @types/express @types/mongoose typegoose
index.ts:
This file, I just have for bootstraping purposes
import app from './app';
const port = 3000;
app.listen(port, (err) =>
if (err)
return console.log(err);
return console.log('Server up and running on ' + port);
);
app.ts:
Here, the actual logic is going on
import UserRepository from './user-repository';
import * as express from 'express';
export class App
public app;
constructor()
this.app = express();
this.config();
this.mountRoutes();
private mountRoutes()
// Definition of the possible API routes
this.app.route('/users')
.get((req, res) =>
var repo = new UserRepository();
// we catch the result with the typical "then"
repo.getUsers().then((x) =>
// .json(x) instead of .send(x) should also be okay
res.status(200).send(x);
);
);
// here with parameter
//
export default new App().app;
user-repository.ts
import * as mongoose from 'mongoose';
import User, UserModel from './models/user.model';
export class UserRepository
constructor()
async getUser(id: number): Promise<User>
// json query object as usual
//
async getUsers(): Promise<User>
return UserModel.find();
user.model.ts
import * as mongoose from 'mongoose';
import prop, Typegoose from 'typegoose';
export class User extends Typegoose
// v
@prop()
userId: number;
@prop()
firstname?: string;
@prop()
lastname: string;
@prop()
email: string;
export const UserModel = new User().getModelForClass(User,
// v
schemaOptions: collection: 'users'
)
As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If you want to programm an API under the following conditions, you can use the provided minimal example below:
Conditions:
NodeJS
Typescript
MongoDB (locally)
Example:
If you are working with typescript, a project structure, similar to this is recommended:
├── dist
| ├── (your compiled JS)
├── src
| ├── models
| | ├── user.model.ts
| ├── user-repository.ts
| ├── app.ts
| ├── index.ts
├── test
| ├── (your tests)
Furthermore, it's required to install the following packages
npm install --save typescript mongoose express @types/express @types/mongoose typegoose
index.ts:
This file, I just have for bootstraping purposes
import app from './app';
const port = 3000;
app.listen(port, (err) =>
if (err)
return console.log(err);
return console.log('Server up and running on ' + port);
);
app.ts:
Here, the actual logic is going on
import UserRepository from './user-repository';
import * as express from 'express';
export class App
public app;
constructor()
this.app = express();
this.config();
this.mountRoutes();
private mountRoutes()
// Definition of the possible API routes
this.app.route('/users')
.get((req, res) =>
var repo = new UserRepository();
// we catch the result with the typical "then"
repo.getUsers().then((x) =>
// .json(x) instead of .send(x) should also be okay
res.status(200).send(x);
);
);
// here with parameter
//
export default new App().app;
user-repository.ts
import * as mongoose from 'mongoose';
import User, UserModel from './models/user.model';
export class UserRepository
constructor()
async getUser(id: number): Promise<User>
// json query object as usual
//
async getUsers(): Promise<User>
return UserModel.find();
user.model.ts
import * as mongoose from 'mongoose';
import prop, Typegoose from 'typegoose';
export class User extends Typegoose
// v
@prop()
userId: number;
@prop()
firstname?: string;
@prop()
lastname: string;
@prop()
email: string;
export const UserModel = new User().getModelForClass(User,
// v
schemaOptions: collection: 'users'
)
As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.
add a comment |
up vote
0
down vote
If you want to programm an API under the following conditions, you can use the provided minimal example below:
Conditions:
NodeJS
Typescript
MongoDB (locally)
Example:
If you are working with typescript, a project structure, similar to this is recommended:
├── dist
| ├── (your compiled JS)
├── src
| ├── models
| | ├── user.model.ts
| ├── user-repository.ts
| ├── app.ts
| ├── index.ts
├── test
| ├── (your tests)
Furthermore, it's required to install the following packages
npm install --save typescript mongoose express @types/express @types/mongoose typegoose
index.ts:
This file, I just have for bootstraping purposes
import app from './app';
const port = 3000;
app.listen(port, (err) =>
if (err)
return console.log(err);
return console.log('Server up and running on ' + port);
);
app.ts:
Here, the actual logic is going on
import UserRepository from './user-repository';
import * as express from 'express';
export class App
public app;
constructor()
this.app = express();
this.config();
this.mountRoutes();
private mountRoutes()
// Definition of the possible API routes
this.app.route('/users')
.get((req, res) =>
var repo = new UserRepository();
// we catch the result with the typical "then"
repo.getUsers().then((x) =>
// .json(x) instead of .send(x) should also be okay
res.status(200).send(x);
);
);
// here with parameter
//
export default new App().app;
user-repository.ts
import * as mongoose from 'mongoose';
import User, UserModel from './models/user.model';
export class UserRepository
constructor()
async getUser(id: number): Promise<User>
// json query object as usual
//
async getUsers(): Promise<User>
return UserModel.find();
user.model.ts
import * as mongoose from 'mongoose';
import prop, Typegoose from 'typegoose';
export class User extends Typegoose
// v
@prop()
userId: number;
@prop()
firstname?: string;
@prop()
lastname: string;
@prop()
email: string;
export const UserModel = new User().getModelForClass(User,
// v
schemaOptions: collection: 'users'
)
As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.
add a comment |
up vote
0
down vote
up vote
0
down vote
If you want to programm an API under the following conditions, you can use the provided minimal example below:
Conditions:
NodeJS
Typescript
MongoDB (locally)
Example:
If you are working with typescript, a project structure, similar to this is recommended:
├── dist
| ├── (your compiled JS)
├── src
| ├── models
| | ├── user.model.ts
| ├── user-repository.ts
| ├── app.ts
| ├── index.ts
├── test
| ├── (your tests)
Furthermore, it's required to install the following packages
npm install --save typescript mongoose express @types/express @types/mongoose typegoose
index.ts:
This file, I just have for bootstraping purposes
import app from './app';
const port = 3000;
app.listen(port, (err) =>
if (err)
return console.log(err);
return console.log('Server up and running on ' + port);
);
app.ts:
Here, the actual logic is going on
import UserRepository from './user-repository';
import * as express from 'express';
export class App
public app;
constructor()
this.app = express();
this.config();
this.mountRoutes();
private mountRoutes()
// Definition of the possible API routes
this.app.route('/users')
.get((req, res) =>
var repo = new UserRepository();
// we catch the result with the typical "then"
repo.getUsers().then((x) =>
// .json(x) instead of .send(x) should also be okay
res.status(200).send(x);
);
);
// here with parameter
//
export default new App().app;
user-repository.ts
import * as mongoose from 'mongoose';
import User, UserModel from './models/user.model';
export class UserRepository
constructor()
async getUser(id: number): Promise<User>
// json query object as usual
//
async getUsers(): Promise<User>
return UserModel.find();
user.model.ts
import * as mongoose from 'mongoose';
import prop, Typegoose from 'typegoose';
export class User extends Typegoose
// v
@prop()
userId: number;
@prop()
firstname?: string;
@prop()
lastname: string;
@prop()
email: string;
export const UserModel = new User().getModelForClass(User,
// v
schemaOptions: collection: 'users'
)
As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.
If you want to programm an API under the following conditions, you can use the provided minimal example below:
Conditions:
NodeJS
Typescript
MongoDB (locally)
Example:
If you are working with typescript, a project structure, similar to this is recommended:
├── dist
| ├── (your compiled JS)
├── src
| ├── models
| | ├── user.model.ts
| ├── user-repository.ts
| ├── app.ts
| ├── index.ts
├── test
| ├── (your tests)
Furthermore, it's required to install the following packages
npm install --save typescript mongoose express @types/express @types/mongoose typegoose
index.ts:
This file, I just have for bootstraping purposes
import app from './app';
const port = 3000;
app.listen(port, (err) =>
if (err)
return console.log(err);
return console.log('Server up and running on ' + port);
);
app.ts:
Here, the actual logic is going on
import UserRepository from './user-repository';
import * as express from 'express';
export class App
public app;
constructor()
this.app = express();
this.config();
this.mountRoutes();
private mountRoutes()
// Definition of the possible API routes
this.app.route('/users')
.get((req, res) =>
var repo = new UserRepository();
// we catch the result with the typical "then"
repo.getUsers().then((x) =>
// .json(x) instead of .send(x) should also be okay
res.status(200).send(x);
);
);
// here with parameter
//
export default new App().app;
user-repository.ts
import * as mongoose from 'mongoose';
import User, UserModel from './models/user.model';
export class UserRepository
constructor()
async getUser(id: number): Promise<User>
// json query object as usual
//
async getUsers(): Promise<User>
return UserModel.find();
user.model.ts
import * as mongoose from 'mongoose';
import prop, Typegoose from 'typegoose';
export class User extends Typegoose
// v
@prop()
userId: number;
@prop()
firstname?: string;
@prop()
lastname: string;
@prop()
email: string;
export const UserModel = new User().getModelForClass(User,
// v
schemaOptions: collection: 'users'
)
As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.
answered Nov 11 at 1:37
Frederic Reisenhauer
155
155
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245096%2fhow-to-set-up-typegoose%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown