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

时间:2017-12-07 16:07:21

标签: javascript node.js mongodb docker

使用typescript我想以这种方式使用mongo db:

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容器中出现异常:

  

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 start,运行已编译的* .ts

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

使用

生成构建
 "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

0 个答案:

没有答案