node.js - 为什么 func 不是 node.js 中的函数?
问题描述
我正在学习mongodb
,在书中,有一个代码
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://127.0.0.1:27017/testdb";
module.exports = function (func) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
else {
console.log("connected");
func(db);
db.close();
}
});
};
我运行这段代码,但抛出错误TypeError: func is not a function
,我用谷歌搜索,但有很多这样的代码,我的 mongodb 版本是 4.0,node.js 版本是 9.10,有什么想法吗?
解决方案
无论func
您传递什么,都必须是一个函数。
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://127.0.0.1:27017/testdb";
module.exports = function (func) { //func must be function, dont pass just a variable
MongoClient.connect(url, function(err, db) {
if (err) throw err;
else {
console.log("connected");
func(db);
db.close();
}
});
};
推荐阅读
- echarts - eCharts 未在某些显示的数据上显示所有数据
- python - 替换文件中文本的有效方法
- python - 在 pyspark 中打印 py_files
- kotlin - Micronaut-data:使用 Kotlin 和 MariaDB 的简单示例无法解析无效查询方法 [更新]
- python - Python + connexion 和 keycloak 集成服务的 Swagger UI 简单示例
- apache-kafka - 卡夫卡服务没有启动
- sql - 如何计算雪花中不同格式的日期差异?
- php - Laravel 应用程序不在 xampp 或实时服务器上运行,但在 php artisan serve 上运行良好
- latex - 使用“visible on =”自动为 tikzpicture 设置动画
- java - Jackson 无法解析 POST 请求的正文