node.js - TypeError: 'connect' 只接受回调
问题描述
当我尝试node index.js
我的错误时TypeError: 'connect' only accepts a callback
,我对如何解决这个问题感到困惑。错误转到 mongo_client.js:165:11
我做了一个关于如何使用 Nodejs 连接到 mongodb 的教程,但是,我发现了一个 ppl 从未遇到过的问题
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient();
const url = 'mongodb://localhost:27107/testedmtdev';
MongoClient.connect(url, function(err, client){
if(err){
console.log('Unable to connect to the Mongodb server. Error : ', err);
} else {
app.listen(3000, ()=>{
console.log('Connected to mongodb, webservice running on port 3000');
})
}
});
我期望“连接到 mongodb,在端口 3000 上运行的 web 服务”的输出
解决方案
这是因为您实际上是MongoClient
在没有参数的情况下调用。
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient(); // <-- your calling the method here
进行更改以仅使用参考
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient; // <-- do *not* call method
https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connect
推荐阅读
- python - 使用pandas python的条形图x轴很乱
- angular - Angular 8:无法在选择框中获取所选值
- python - 我如何访问 gsp 电子表格中的工作表名称。工作表()
- airflow - 在时区感知 DAG 中,next_ds 在夏令时期间的行为是否正确?
- ios - 我需要以编程方式在智能电视上显示来自 iphone 的视频
- c# - Audit.Net MVC - 如何解决 NullReferenceException?
- reactjs - 为什么在输入更改时重新渲染功能组件中的所有元素
- javascript - moment.utc('date string').format('DD-MMM-YYYY') 返回上一个日期
- c# - 在不同的机器 C# 中生成和签署证书
- jquery - JQuery .nextUntil() 选择标题进入 li