如何使用 es6 样式导入导入 MongoDB? [英] How to import MongoDB using es6 style imports?

查看:41
本文介绍了如何使用 es6 样式导入导入 MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个简单的问题.我正在尝试使用 es6 import-from 样式导入 MongoDB.如果我使用 node require 导入它可以正常工作.

Hopefully this is a simple question. I am trying to import MongoDB using the es6 import-from style. If I import using node require it works fine.

let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;

但是如果我以 es6 的方式导入它,它就会中断而没有错误或日志.

But if I import it the es6 way it breaks without errors or logs.

import {MongoClient} from 'mongodb';

但是它在编译/运行时不会中断,它只会在我尝试使用 MongoClient 执行任何操作时中断.

But it doesn't break when compiling/running it only breaks when I try to do anything with MongoClient.

这是我的数据库管理器类-

Here is my Db Manager class-

import {MongoClient} from 'mongodb';

export class DbManager {

  constructor() {
    console.log('Constructing DB Connection');
  }

}

当我运行我的服务器时,我从其他经理和事件那里得到了一些日志.

When I run my server I get several logs from other managers and events.

mycomputer myuser$ ./start.sh
Server Constructing
Route Manager Constructing
Initializing Route: Static
Constructing DB Connection
http server started on port: 8000

但是,如果我执行 MongoClient 的 console.log,则根本没有输出.

But if I do a console.log of the MongoClient there is simply no output.

import {MongoClient} from 'mongodb';

export class DbManager {

  constructor() {
    console.log('Constructing DB Connection');
    console.log(MongoClient);
  }

}

输出看起来像这样-

mycomputer myuser$ ./start.sh
mycomputer myuser$

没有编译错误,所以我不明白为什么这不起作用.此外,我不明白为什么没有任何日志!这是最后发生的事情之一,至少应该有日志,直到我想.如果您想在此处查看我的 start.sh 脚本(又快又脏,请不要评判我):

There are no compile errors so I don't understand why this isn't working. Furthermore, I don't understand why there aren't any logs! This is one of the last things that happens, there should at least be logs up until that point I'd think. If you'd like to see my start.sh script here it is (quick and dirty, don't judge me):

tsc
echo "var System = require('systemjs');" > dist/final.js
babel dist/typescript.js >> dist/final.js
echo "System.import('main');" >> dist/final.js
node dist/final.js

编辑

在等待(希望)回应的同时继续寻找答案.我正在查看生成的 final.js,如果在文件中的任何地方使用了 MongoClient,System.register 函数调用看起来像这样 -

Continuing to search for the answer while waiting (hoping) for a response. I'm taking a look at the resulting final.js and if MongoClient is used anywhere in the file the System.register function call looks like this-

System.register("db/db.manager", ["mongodb"] ...

如果我不使用它(即使我导入它),它也不会显示 mongodb.

And if I don't use it (even if I import it) it does not show mongodb.

System.register("db/db.manager", [] ...

这就解释了为什么什么也不会发生.尝试导入 mongodb 时出了点问题.还不确定该怎么做.

That would explain why nothing would happen. Something is wrong with trying to import mongodb. Not sure yet what to do.

编辑编辑

找到了解决办法.一个我并不感到兴奋,但也许这就是它必须的方式.

Found a solution. One i'm not thrilled with but maybe it's just the way it has to be.

我认为我不能依赖 es6 导入.看起来我可以用它来导入 typedef 而不是实际的模块.我是如何解决这个问题的-

I don't think I can rely on es6 imports. It looks like I can use it to import the typedefs but not the actual module. How I got around this is like this-

import {Db as MongoDb, MongoClient} from 'mongodb';
let mongodb = require('mongodb');
let mongoClient: MongoClient = mongodb.MongoClient;

很多额外的工作.如果有其他方法请告诉我.

A lot of extra work. If there's another way please let me know.

推荐答案

import { MongoClient } from 'mongodb';

只是从 node_modules/@types/mongodb/index.d.ts 导入类型定义

just imports type definition from node_modules/@types/mongodb/index.d.ts

import * as mongodb from 'mongodb';

从 node_modules/mongodb/index.js 导入所有内容,与

imports everything from node_modules/mongodb/index.js and its the same as

let mongodb = require('mongodb');

这篇关于如何使用 es6 样式导入导入 MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆