繁体   English   中英

Node.js 异步/等待模块导出

[英]Node.js Async/Await module export

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

// 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)

这是需要模块的js

// test.js

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

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

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

您必须同步导出,因此无法直接导出clientdb 但是,您可以导出解析为clientdb的 Promise :

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)不是一个合适的错误处理程序,如果你不能处理错误就崩溃

@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'});
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM