Autorun
Autorun

Reputation: 319

How to use mongoose on angular2/browser

I am new to MEAN and would like to seek your help.

Since I am using Mongoose on the server side and have a lot of schema, I would like to reuse the schema in the Angular2 browser side in my form input. As Mongoose is isomorphic now, I think it should be doable. If possible, I would like to use schema, document and validation on the client side with Mongoose/Angular2. I will not connect to a db from the browser.

Question1: how can I use the Mongoose on the broswer side?

I browsed around and am thinking this approach which may not correct.

  1. npm install mongoose --save
  2. typings install --global --save dt~mongoose (also mpromise, mongoose-promise, mongodb, node)
  3. on app.module.ts, import * as mongoose from 'mongoose';
  4. copy those ....\typings\globals* to ....\node_modules (not sure I need this step, but just try it out)

When I did 'ng s', I got these error.

Question 2: how can I resolve these errors below? I also tried to npm install --save events, npm install --save stream and typings them.

ERROR in [default] D:\MEAN\projects\fhir\fhirapp\node_modules\mongodb\index.d.ts:4:29 Cannot find module 'events'.

ERROR in [default] D:\MEAN\projects\fhir\fhirapp\node_modules\mongodb\index.d.ts:102:18 Cannot find name 'Buffer'.

ERROR in [default] D:\MEAN\projects\fhir\fhirapp\node_modules\mongodb\index.d.ts:1127:44 Cannot find namespace 'NodeJS'.

ERROR in [default] D:\MEAN\projects\fhir\fhirapp\node_modules\mongoose\index.d.ts:6:26 Cannot find module 'stream'.

ERROR in [default] D:\MEAN\projects\fhir\fhirapp\node_modules\mongoose\index.d.ts:13:29 Cannot find name 'global'.

My typings.json

{
  "globalDependencies": {
    "mongodb": "registry:dt/mongodb#2.1.0+20160602142941",
    "mongoose": "registry:dt/mongoose#4.5.9+20160922172925",
    "mongoose-promise": "registry:dt/mongoose-promise#4.5.4+20160822161941",
    "mpromise": "registry:dt/mpromise#0.5.4+20160316155526",
    "node": "registry:dt/node#6.0.0+20160923124626"
  }
}

You help is high appreciated. Thanks in advance.

Upvotes: 2

Views: 2916

Answers (1)

WiredPrairie
WiredPrairie

Reputation: 59763

With TypeScript 2.0.3 installed (which if you're doing Angular 2.0+, you should be using), I used the following package.json:

npm install mongoose core-js --save
npm install @types/mongoose @types/core-js @types/node typescript --save-dev

{
  "name": "stackoverflow_mongoose",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "license": "ISC",
  "dependencies": {
    "core-js": "^2.4.1",
    "mongoose": "^4.6.1"
  },
  "devDependencies": {
    "@types/core-js": "^0.9.34",
    "@types/mongoose": "^4.5.36",
    "@types/node": "^6.0.41",
    "typescript": "^2.0.3"
  }
}

I created an tsconfig.json file:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false
    }, 
    "exclude": [
        "node_modules"
    ]
}

And one sample file, index.ts:

import * as mongoose from "mongoose";

mongoose.connect("sample");

There are no compile errors. You shouldn't need to use typings anymore. Instead, you can use @types.

Upvotes: 3

Related Questions