Node.js mongodb:db.collection不是一个函数

打字稿我想用这种方式使用mongo数据库:

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

但是这段代码导致docker容器exception:

TypeError:db.collection不是一个函数

我已经添加了一个console.log(db) ,结果是一个MongoClient对象:

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

从package.json我已经安装

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

从我的Dockerfile我只安装生产依赖项:

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

npm开始,运行编译的* .ts

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

build立与生产

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

tsconfig.release.json是

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

和tsconfig.json是

 { "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__/**/*" ] } 

我做错了什么?

解决了!!

我已经安装了mongodb 2.2.0,现在可以运行,请参阅https://stackoverflow.com/a/47662979/2936170