我创建了NodeJS V8应用程序,并使用mongoDB本地服务器。
我有mongoDB版本^2.2.34并连接到DB
let mongodb = require('mongodb');
let mongoClient = mongodb.MongoClient;
let connection = mongoClient.connect('mongodb://localhost:27017/Test');
let getCollection = function (c) {
return connection.then(function (db) {
return db.collection(c);
});
};啊,真灵。我将我的mongoDB版本更新为^3.0.1,并且有错误
(node:16320) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: db.collection is not a function为什么在新版本中它不是工作,以及我如何更改代码?
发布于 2018-01-26 15:39:00
您期望connect()仍然返回一个db。阅读Mongo升级指南 (重点地雷):
3.0有什么新鲜事?
MongoClient.connect 现在返回一个客户端而不是DB.。考虑到这一点,您的代码将如下所示:
const {
MongoClient
} = require("mongodb");
const connection = MongoClient.connect("mongodb://localhost:27017");
function getCollection(c) {
return connection
.then(client => client.db("Test"))
.then(db => db.collection(c));
}https://stackoverflow.com/questions/48464522
复制相似问题