【发布时间】:2020-08-25 06:32:52
【问题描述】:
我在用于获取用户的文件中有一个函数,该函数中的 console.log() 可以工作,但是从另一个文件中使用它时返回未定义。
const db = client.db('TestDatabase');
const collection = db.collection('users');
exports.getUser = function(_id) {
collection.findOne({
_id: _id
}, (err, data) => {
if (err) {
console.log(err);
return;
}
//console.log(data); // Logs the proper data.
return data; // Return data?
});
}
使用它的文件来自:
const database = require('./db.js');
let user = database.getUser(_id);
console.log(user); // This logs "undefined"
【问题讨论】:
标签: javascript node.js node-modules