4

With typescript i want use mongo db in this way:

import { config } from '../config';
import { MongoClient } from 'mongodb';

MongoClient.connect(config.mdb_uri, (err, db) => {
    if ( err ){
       console.log(err);
       return;
    }

    db.collection('mycollection').updateOne( 
      { '_id': 'x' },
      { $set: { 'info': 'OK' } },
      { upsert: true }
    );
});

but this code cause an exception into a docker container:

TypeError: db.collection is not a function

i have added a console.log(db) and the result is a MongoClient object:

MongoClient {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  s:
   { url: 'mongodb://database:27017/mydb',
     options:
      { socketOptions: {},
        read_preference_tags: null,
        readPreference: [Object],
        dbName: 'dashboard',
        servers: [Array],
        server_options: [Object],
        db_options: [Object],
        rs_options: [Object],
        mongos_options: [Object],
        socketTimeoutMS: 360000,
        connectTimeoutMS: 30000,
        promiseLibrary: [Function: Promise] },
     promiseLibrary: [Function: Promise],
     dbCache: {},
     sessions: [] },
  topology:
   Server {
     domain: null,
     _events:
      { serverOpening: [Function],
        serverDescriptionChanged: [Function],
        serverHeartbeatStarted: [Function],
        serverHeartbeatSucceeded: [Function],
        serverHeartbeatFailed: [Function],
        serverClosed: [Function],
        topologyOpening: [Function],
        topologyClosed: [Function],
        topologyDescriptionChanged: [Function],
        joined: [Function],
        left: [Function],
        ping: [Function],
        ha: [Function],
        authenticated: [Function],
        error: [Function],
        timeout: [Function],
        close: [Function],
        parseError: [Function],
        open: [Object],
        fullsetup: [Object],
        all: [Object],
        reconnect: [Function] },
     _eventsCount: 22,
     _maxListeners: undefined,
     clientInfo:
      { driver: [Object],
        os: [Object],
        platform: 'Node.js v8.4.0, LE' },
     s:
      { coreTopology: [Object],
        sCapabilities: null,
        clonedOptions: [Object],
        reconnect: true,
        emitError: true,
        poolSize: 5,
        storeOptions: [Object],
        store: [Object],
        host: 'database',
        port: 27017,
        options: [Object],
        sessionPool: [Object],
        promiseLibrary: [Function: Promise] } } }

from package.json i have installed

"dependencies": {
  ...
  "mongodb": "^3.0.0-rc0",
  ...
},
"devDependencies": {
  ...
  "@types/mongodb": "^2.2.16",
  ...
}

from my Dockerfile i install only the production dependencies:

RUN npm install --only=production
CMD ["npm", "start"]

npm start, run the compiled *.ts

"scripts": {
  ...
  "start": "node build/main.js",
  ...
 }

the build is produced with

 "build": "tsc -p tsconfig.release.json"

tsconfig.release.json is

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "declaration": false,
    "removeComments": true,
    "jsx": "react"
  },
  "include": [
    "src/**/*"
  ]
}

and tsconfig.json is

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "jsx": "preserve",
    "allowJs": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "declaration": true,
    "outDir": "build",
    "rootDir": "src",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "src/**/*",
    "__tests__/**/*"
  ]
}

what i'm doing wrong?

SOLVED!!

i have installed mongodb 2.2.0 and now works, see https://stackoverflow.com/a/47662979/2936170

ar099968
  • 6,963
  • 12
  • 64
  • 127

0 Answers0