Node.js 异步/等待module导出

IT技术 javascript node.js module async-await
2021-03-19 05:19:34

我对module创建有点陌生,想知道 module.exports 并等待异步函数(例如 mongo 连接函数)完成并导出结果。在module中使用 async/await 正确定义了变量,但是当尝试通过要求module来记录它们时,它们显示为未定义。如果有人能指出我正确的方向,那就太好了。这是我到目前为止的代码:

// module.js

const MongoClient = require('mongodb').MongoClient
const mongo_host = '127.0.0.1'
const mongo_db = 'test'
const mongo_port = '27017';

(async module => {

  var client, db
  var url = `mongodb://${mongo_host}:${mongo_port}/${mongo_db}`

  try {
    // Use connect method to connect to the Server
    client = await MongoClient.connect(url, {
      useNewUrlParser: true
    })

    db = client.db(mongo_db)
  } catch (err) {
    console.error(err)
  } finally {
    // Exporting mongo just to test things
    console.log(client) // Just to test things I tried logging the client here and it works. It doesn't show 'undefined' like test.js does when trying to console.log it from there
    module.exports = {
      client,
      db
    }
  }
})(module)

这是需要module的js

// test.js

const {client} = require('./module')

console.log(client) // Logs 'undefined'

我对 js 相当熟悉,并且仍在积极学习和研究诸如 async/await 和类似功能之类的东西,但是是的...我真的想不通

2个回答

你必须同步输出,所以它不可能出口clientdb直接。但是,您可以导出解析为client的 Promise db

module.exports = (async function() {
 const client = await MongoClient.connect(url, {
   useNewUrlParser: true
 });

  const db = client.db(mongo_db);
  return { client, db };
})();

那么你可以将它导入为:

const {client, db} = await require("yourmodule");

(必须在异步函数本身中)

PS:console.error(err)不是一个合适的错误处理程序,如果你不能处理错误就崩溃

谢谢。所以我需要在我想查询数据库的每个函数中都需要这个?感觉非常“样板”
2021-04-23 05:19:34
@brocococonut 很高兴为您提供帮助 :)
2021-04-24 05:19:34
因为您返回一个函数,该方法将为每个需求打开一个新连接。不是吗?
2021-04-30 05:19:34
@DevAKS const abc = await import("./sample-module");
2021-05-07 05:19:34
啊,我明白了.. 最后我将导出需要 mongo 连接的东西,而不是导出连接本身。不过确实有道理,谢谢:)
2021-05-12 05:19:34

@Jonas Wilms 上面提供的解决方案正在运行,但每次我们想要重用连接时都需要在异步函数中调用 requires 。另一种方法是使用回调函数返回 mongoDB 客户端对象。

mongo.js:

const MongoClient = require('mongodb').MongoClient;

const uri = "mongodb+srv://<user>:<pwd>@<host and port>?retryWrites=true";

const mongoClient = async function(cb) {
    const client = await MongoClient.connect(uri, {
             useNewUrlParser: true
         });
         cb(client);
};

module.exports = {mongoClient}

然后我们可以在不同的文件(快速路由或任何其他 js 文件)中使用 mongoClient 方法。

应用程序.js:

var client;
const mongo = require('path to mongo.js');
mongo.mongoClient((connection) => {
  client = connection;
});
//declare express app and listen....

//simple post reuest to store a student..
app.post('/', async (req, res, next) => {
  const newStudent = {
    name: req.body.name,
    description: req.body.description,
    studentId: req.body.studetId,
    image: req.body.image
  };
  try
  {

    await client.db('university').collection('students').insertOne({newStudent});
  }
  catch(err)
  {
    console.log(err);
    return res.status(500).json({ error: err});
  }

  return res.status(201).json({ message: 'Student added'});
};
这将创建多个客户端......不确定是否需要。如果您在加载 db 客户端之前调用它,API 端点也会响应错误。
2021-05-07 05:19:34