有关在ES6的规范下连接数据库的问题
发布于 3 年前 作者 littlebird-maker 2522 次浏览 来自 问答

问题大体描述:大佬们。想请教一下在es6的规范下,咋连接数据库呢? 结合自身实际的说明: 我在网上查到commonjs和es6的最大的不同就是导入包的方式不同。而且网上绝大多数教程都是在commonjs的规范下使用require导入数据库包进行连接。所以我根据网上的一些经验介绍,在es6的规范代码里使用除了导包的方法不同之外其他方式与commonjs相同的方式进行连接,但是会报错。希望大佬能够帮忙解答疑惑 具体代码和报错如下: import mongodb from ‘mongodb’; //引进数据库模块 let mongoClient = mongodb.MongoClient; const url = ‘mongodb://127.0.0.1:27017’; class Db{ /* @database 数据库名字 @colName 集合名字 / constructor(database,colName){ this.database = database; this.colName = colName; } / 查询数据 @obj 查询的对象 @cb 回调函数 */ find(obj,cb){ let that = this; mongoClient.connect(url,function(err,dbs){ if(err){ //判断是否出错,err为null则成功 console.log(err); }else{ let db = dbs.db(that.database); db.createCollection(that.colName).then(function(collection){ collection.find(obj).toArray(function(err,data){ dbs.close(); //关闭数据库 if(err){ console.log(err); cb(); }else{ typeof cb ==‘function’ && cb(data); } }); }) }

    })
}
/*
    插入一条数据
    @obj  查询的对象
    @cb   回调函数
 */
insertOne(obj,cb){
    let that = this;
    mongoClient.connect(url,function(err,dbs){
        if(err){
            console.log(err);
        }else{
            let db = dbs.db(that.database);
            db.createCollection(that.colName).then(function(collection){
                collection.insertOne(obj,function(err){
                    dbs.close();
                    if(err){
                        console.log(err);
                    }else{
                        typeof cb =='function' && cb();
                    }
                });
            })
        }
    })
}
/*
    插入多条数据
    @obj  查询的对象
    @cb   回调函数
 */
insertMany(obj,cb){
    let that = this;
    mongoClient.connect(url,function(err,dbs){
        if(err){
            console.log(err);
        }else{
            let db = dbs.db(that.database);
            db.createCollection(that.colName).then(function(collection){
                collection.insertMany(obj,function(err){
                    dbs.close();
                    if(err){
                        console.log(err);
                    }else{
                        typeof cb =='function' && cb();
                    }
                });
            })
        }
    })
}
/*
    删除一条数据
    @obj  查询的对象
    @cb   回调函数
 */
deleteOne(obj,cb){
    let that = this;
    mongoClient.connect(url,function(err,dbs){
        if(err){
            console.log(err);
        }else{
            let db = dbs.db(that.database);
            db.createCollection(that.colName).then(function(collection){
                collection.deleteOne(obj,function(err){
                    dbs.close();
                    if(err){
                        console.log(err);
                    }else{
                        typeof cb =='function' && cb();
                    }
                });
            })
        }
    })
}
/*
    删除多条数据
    @obj  查询的对象
    @cb   回调函数
 */
deleteMany(obj,cb){
    let that = this;
    mongoClient.connect(url,function(err,dbs){
        if(err){
            console.log(err);
        }else{
            let db = dbs.db(that.database);
            db.createCollection(that.colName).then(function(collection){
                collection.deleteMany(obj,function(err){
                    dbs.close();
                    if(err){
                        console.log(err);
                    }else{
                        typeof cb =='function' && cb();
                    }
                });
            })
        }
    })
}
/*
    更新一条数据
    @obj  查询的对象
    @obj1  更改内容
    @cb   回调函数
 */
update(obj,newobj,cb){
    let that = this;
    mongoClient.connect(url,function(err,dbs){
        if(err){
            console.log(err);
        }else{
            let db = dbs.db(that.database);
            db.createCollection(that.colName).then(function(collection){
                collection.update(obj,newobj,function(err){
                    dbs.close();
                    if(err){
                        console.log(err);
                    }else{
                        typeof cb =='function' && cb();
                    }
                });
            })
        }
    })

}
/*
    更新多条数据
    @obj  查询的对象
    @obj1  更改内容
    @cb   回调函数
 */
updateMany(obj,obj1,cb){
    let that = this;
    mongoClient.connect(url,function(err,dbs){
        if(err){
            console.log(err);
        }else{
            let db = dbs.db(that.database);
            db.createCollection(that.colName).then(function(collection){
                collection.updateMany(obj,obj1,function(err){
                    dbs.close();
                    if(err){
                        console.log(err);
                    }else{
                        typeof cb =='function' && cb();
                    }
                });
            })
        }
    })

}

} export default Db;

报错:

ERROR in ./node_modules/mongodb/lib/url_parser.js Module not found: Error: Can’t resolve ‘dns’ in ‘C:\Users\86136\Desktop\writetogether2\collaborative-editor\node_modules\mongodb\lib’ @ ./node_modules/mongodb/lib/url_parser.js 7:12-26 @ ./node_modules/mongodb/lib/operations/connect.js @ ./node_modules/mongodb/lib/mongo_client.js @ ./node_modules/mongodb/index.js @ ./modules/db.js @ ./demo/index.js

ERROR in ./node_modules/mongodb/lib/core/uri_parser.js Module not found: Error: Can’t resolve ‘dns’ in ‘C:\Users\86136\Desktop\writetogether2\collaborative-editor\node_modules\mongodb\lib\core’ @ ./node_modules/mongodb/lib/core/uri_parser.js 4:12-26 @ ./node_modules/mongodb/lib/core/index.js @ ./node_modules/mongodb/index.js @ ./modules/db.js @ ./demo/index.js

ERROR in ./node_modules/mongodb/lib/core/auth/gssapi.js Module not found: Error: Can’t resolve ‘dns’ in ‘C:\Users\86136\Desktop\writetogether2\collaborative-editor\node_modules\mongodb\lib\core\auth’ @ ./node_modules/mongodb/lib/core/auth/gssapi.js 2:12-26 @ ./node_modules/mongodb/lib/core/index.js @ ./node_modules/mongodb/index.js @ ./modules/db.js @ ./demo/index.js

12 回复

webpack 或者 typescript 编译一下

@ganshiqingyuan 大兄弟,实不相瞒。这个就是webpack-dev-server编译的

   这个问题其实是由于项目需要我在网上找到了一份协同编辑源码,这份源码是使用nodejs编写的。但是他的服务器用的是express框架。客户端是js(es6规范),css,html一起用webpack打包的构造。然后我希望能使前后端连接上数据库。但是目前服务器是没有任何问题的可以链接到mongodb,但是前端总是会或多或少的出现一堆问题。所以希望能请教哪位了解es6规范下链接数据库的大神能给出一些宝贵的意见。源码地址如下(同时也非常感谢源码的作者的开源分享贡献):
		https://link.zhihu.com/?target=https%3A//github.com/we-miks/collaborative-editor
		相关文章:https://zhuanlan.zhihu.com/p/131572523

@ganshiqingyuan 我大概知道是为什么了,他使用webpack打包运行的。webpack应该是打包不了服务端的代码的

@littlebird-maker 我没太细看 = =,,webpack可以打包nodejs代码的啊。。但是客户端理论上连不上数据库啊,他控制不了socket

dns 是nodejs内置的模块 感觉你方向走错了

@ganshiqingyuan 这个是webpack打包不了连接数据库的部分,我在Google上查到了这个报错。客户端确实连接不上数据库但是可以用websocket和后端实时交换数据。目前已经解决了,运行没有问题

@netwjx 其实就是webpack无法打包mongodb中的dns这几个模块,客户端和服务端分离用websocket进行通信,把需要保存的数据放到服务端,让服务端链接数据库就OK啦

@ganshiqingyuan 大神有时间的话,可以帮萌新解决一下另外一个有关echarts的问题嘛?

在webpack.config.js 添加如下代码:

const nodeExternals = require("webpack-node-externals");
module.exports = {
	externals: [ nodeExternals() ]
}

希望能够帮助到你

回到顶部